How to insert html block in #page Razor - c#

Is it possible to insert html block in #page Razor page in the new ASP.NET core?
I want to insert html block the way I'm doing it in Angular - like when you insert components. Here's the problem: only in the Layout page the IntelliSense allows me to use #RenderSection
Also, the #RenderPage is not recognizable in the IntelliSense.
If I'm using the #RenderPage it also gives error
So is it possible / is there a way to use the #RenderPage or to insert html blocks within .cshtml files with Razor?
Thanks

If you need to render another view into a view, the method that you need is
#Html.Partial("partialViewName").
This allow you to send a Model and other parameters.

Use the #Html.Partial() method. You can provide it with a Partial View and Model for that view.
Example:
#Html.Partial("_NavBar")
or
#Html.Partial("_NavBar, Model.Customer")

Related

How to display razor and C# code in view in MVC

I am using MVC and razor for a style Guide. I need to display the code in the view so others can use it on the site.
I need to display the code without the Razor helper executing the function.
I am trying to do this via a Razor helper. I want this helper to render the button as well as display the exact MVC Razor helper code in the view (rather than have it display the outputted HTML from the helper).
#helper ShowCode() {
<div>
#Html.TextBox("textBox")
</div>
}
Then I call the helper below and try and display the code:
// render the button
#ShowCode()
// display the button code
<pre>
<code>
#ShowCode().ToString()
</code>
</pre>
However, this displays the following:
I need it to display the following code instead of the HTML:
<div>
#Html.TextBox("textBox")
</div>
How can I display the code within the Razor helper without it being outputted as html? (I want to display exactly what is in the Razor helper). Is this possible?
You can't actually do it like that. Razor returns HTML there is no way to get actual Razor template code as string.

Razor tag inside Html.Raw(...)

Is it possible to render Razor in Html.Raw()? I have a dynamic page being generated that uses the Html.Raw() method to render the page that is created in the controller. My raw html has razor tags in it. That is, I am trying to generate a link by
#Html.Raw(Model.myRawHtmlContainingRazorTags);
where Model.myRawHtmlContainingRazorTags contains
<html>
...
...
#Model.TheLink
...
...
<html>
I need the value of Model.TheLinkto be rendered when #Html.Raw(Model.myRawHtmlContainingRazorTags) is called within the cshtml.
You may have some architectural issues that lead you to have some razor code on your Model property.
Anyway, you can do it by using some external libs such RazorTemplates.
Here is a sample :
var template = Template.Compile(Model.myRawHtmlContainingRazorTags);
#Html.Raw(template.Render(Model));

C# ASP.net render HTML String

I have a bit of code in my markup like this :
<% Response.Write(((String)objRow["Post"]).Trim()); %>
The value in objRow["Post"] is HTML markup - similar to this:
<p><span style="color: #ff0000;">asdsada</span></p>
The content of which was created using tinymce. I now want to render the resultant html - basically a view function of a previously created save function.
At the moment, my markup literally spits out the HTML you see there onto my website - but what I really want is a red line of text saying asdsada.
Please help
it sounds like the value you are trying to display is already html encoded. try this:
<%= HttpUtility.HtmlDecode((string)objRow["Post"]) %>
My version of Asp.net is little bit higher, but this might help someone, who uses .net Core 2.0 MVC.
I am using the following approach for rendering html in my Asp.Net Core 2.0 MVC project (the html is created via Tiny MCE and saved in the database, after that it is rendered on the Razor View).
Please, try these two steps:
1) include System.Net in _ViewImports.cshtml:
#using System.Net
#addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
2) render the html using HtmlDecode in your View.cshtml:
#Html.Raw(#WebUtility.HtmlDecode(Model.MyHtmlData))
This works for me, hope it helps!
But without HtmlDecode, Html.Raw displayed MyHtmlData with html tags (didn't render html, just displayed it). I guess, that is because the data was HTML encoded already, as Zdravko Danev mentioned:
#Html.Raw(Model.MyHtmlData)
You can use literal control in asp.net to display the text and it will render it accordingly.
literal1.text = htmlcodeuwanttodisplay
Did you try something like
Server.HtmlEncode("<p><span style='color: #ff0000;'>asdsada</span></p>");

mvc - allow asp tags in chtml pages

Is there anyway to allow asp tags in chtml MVC pages? Do I have to declare something in the view itself? I know its possible to have an aspx page in the solution, but I would like to combine controls in the chtml page.
Example:
#using (Html.BeginForm())
{
<asp:calander>...
etc
Actually Razor does not support WebForm controls, but you need to see Scott Hanselman's example if you really want to mix n match
http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx

checkbox list example in mvc3 Razor,

I am converting my application in razor template,i used the Html.CheckBoxlist in one of my views.
Please suggest me how can I achieve this in razor
Here is my code:
#Html.CheckBoxlist("abc", Model.CheckBoxListItems)
Razor is simply a view engine it has nothing to do with the methods you call.
If you have such an html helper method, it should work with #Html.CheckBoxlist(parameters)

Categories