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.
Related
I am trying to render Partial View at the time of rendering HTML String. I am using KENDO UI Editor from where I can insert Code Snippets like this
#(Html.Kendo().Editor()
.Tools(tools => tools
.Clear()
.Snippets(snippets => snippets
.Add("Institute List", "#Html.Action('CourseTags', 'Services')")
)
This will create an HTMLSTRING which can be rendered using
#MvcHtmlString.Create(HttpUtility.HtmlDecode(HTMLSTRING))
But it does not execute this #Html.Action('CourseTags', 'Services') and it simply writes as it is.
is there anything I am missing or doing wrong?
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")
This is the MVC 5 razor view code: ForestView.cshtml
#model Forest.Tree
#{
var resultHtml = string.Empty;
}
<div id="divTreeSearch>
#(Html.Kendo().PanelBar().
Name("panelbar")
.Items(panelbar =>
{panelbar.Add()
.Content(#<text>#Html.Partial("_TreeSearch", Model, ViewData)</text>);}))
</div>
<div id="divTreeSearchResult">
#if(Model.TreeResultObj != null)
{
resultHtml = Html.ContentFromPartial("_TreeReport", Model.TreeResultObj);
#Html.Raw(resultHtml); -- Not working
#Html.Raw(HttpUtility.HtmlDecode(resultHtml)); -- Not Working
Html.Raw(resultHtml); -- Not working
Html.Raw(HttpUtility.HtmlDecode(resultHtml)); -- Not Working
Model.resultStringSaved = resultHtml;
#Html.DisplayText("resultStringSaved"); -- Not Working
#Html.Raw("<text>Test</text>") -- Even this is not working
}
#Html.Raw(Model.resultStringSaved) -- Not Working
#Html.Raw(HttpUtility.HtmlDecode(Model.resultStringSaved)) -- Not Working
#Html.DisplayText("resultStringSaved") -- Not Working
#Html.Raw("<text>Test</text>") -- This is Working
</div>
ForestView.cshtml - #model Forest.Tree
_TreeSearch.cshtml - #model Forest.Tree
_TreeReport.cshtml - #model Forest.SearchData.Results
The projerty TreeResultObj in the model Forest.Tree is of type Forest.SearchData.Results
The ForestView.cshtml is the main view which loads initially and displays the search inputs from the _TreeSearch partial
When search criteria entered and a 'search' button is clicked (all this is from the _TreeSearch) - a ajax call is make and the TreeSearch(id tree) action is called
The action again returns the main 'ForestView' - however now the model property 'TreeResultObj' is populated. so the code within the 'if conditon' in the 'ForestView' executed and calls another partial to get the content back as HTML string, which is saved in the 'resultHtml' variable
At this point I can see the Html Sting like "<Text>blah blah blah</text>"
However trying to display the HTML string below the search panel in the main 'ForestView' is not working - I have tried almost every possible way.
Any text within the if condition is not rendered - it is an ajax call so there is no page refresh - I can see the HTML string value and also save it as a Model property but cannot get to display it in the main view.
Any help would be much appreciated. Thanks in advance.
At that point, you are just invoking a method and ignoring the result. Try:
#: #Html.Raw(resultHtml)
The #: switches to output mode. Note: if you had used something that was clearly markup, it would have switched automatically. For example:
<div>#Html.Raw(resultHtml)</div>
I was also facing the same issue but I could make it to working. I wanted to open a div start and conditionally end it within a for loop.
Put any dummy HTML element before the #Html.Raw
<div id="divTreeSearchResult">
#if(Model.TreeResultObj != null)
{
resultHtml = Html.ContentFromPartial("_TreeReport", Model.TreeResultObj);
<span>dummySpan</span>
#Html.Raw(resultHtml);
#Html.Raw(HttpUtility.HtmlDecode(resultHtml));
#Html.Raw(resultHtml);
#Html.Raw(HttpUtility.HtmlDecode(resultHtml));
Model.resultStringSaved = resultHtml;
#Html.DisplayText("resultStringSaved");
#Html.Raw("<text>Test</text>")
}
#Html.Raw(Model.resultStringSaved)
#Html.Raw(HttpUtility.HtmlDecode(Model.resultStringSaved))
#Html.DisplayText("resultStringSaved")
#Html.Raw("<text>Test</text>")
</div>
After you place a dummy html element there all the below #Html.Raw will work
Happy Coding
Tarak
1 - And the content from render should be violating HTML syntax and Razor normally doesn't render wrong HTML content.
2 - Enclose the HTML.Raw(resultHtml) into dev.
After zillions of tests in a full-day:
Change use of:
#Html.Raw(Html.DisplayFor(modelItem => item.Mensagem))
by:
#Html.Raw(Html.DisplayTextFor(modelItem => item.Mensagem))
You will get the rendered HTML withou any <>!!!
In this Razor syntax:
<a asp-controller="Home" asp-action="Index">Home</a>
#foreach (LinkNodeModel link in Model.ControlActions)
{
link.LinkTree();
}
The "Home" link renders just fine, but the manually rendered <a> strings don't get turned into a valid link.
LinkTree() is implemented like this:
return $"<a asp-controller=\"{Controller}\" asp-action=\"{Action}\">{Name}</a>";
When I print the links with the #link.LinkTree(), the output contains a line with just the code displayed, which doesn't link.
With #Html.Raw(link.LinkTree()) I get the links, but they are not clickable as they actually print the asp-controller/asp-action attributes to the HTMLinstead of generating the href.
Is it possible to generate and render links like these dynamically? And if so, how?
HTML code, or actually any text, returned from methods is not processed by the Razor engine, so you cannot use HTML tag helpers here.
What you can do however is call the “classic” HtmlHelper.ActionLink method (or one of the more helpful extension methods) to return a properly rendered a tag for a controller action.. Since it’s just a normal method, you can call it within your own method.
For example, you could pass in the IHtmlHelper object into your method:
#foreach (LinkNodeModel link in Model.ControlActions)
{
link.LinkTree(#Html);
}
And then in your method, just use a ActionLink overload to create the link:
public IHtmlContent LinkTree(IHtmlHelper helper)
{
return helper.ActionLink(Name, Action, Controller);
}
Alternatively, you can also expose those three properties on your object and write the link properly with Razor:
#foreach (LinkNodeModel link in Model.ControlActions)
{
<a asp-controller="#link.Controller" asp-action="#link.Action">#link.Name</a>
}
The tag helpers, which convert <a asp-controller="$controller" asp-action="$action"> to <a href="/$controller/$action"> are opt-in as described in Introducing TagHelpers in ASP.NET MVC 6, so you'll need to configure your application to use them:
This is best placed in the _ViewImports.cshtml file, which is a new Razor file also introduced in ASP.NET 5:
#addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
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));