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));
Related
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.
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")
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>");
So I'm trying to create a custom editor so that for a DataType of "Duration" a textbox appears with a masked format of HH:MM:SS.
I've created a very simple piece of code so far
#Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { #class = "text-box single-line", type = "duration" })
<script>
$(document).ready(function () {
$("##Html.NameFor(c => c)").mask("00:00:00");
});
</script>
This is in my ~/Views/Shared/EditorTemplates/Duration.cshtml file. However it requires an additional javascript to be loaded (maskedInput.js).
Is there any razor includes I can use here so that I can include the maskedInput.js file once and only once in a page load. I realise I could add it to the parent page the editor will be on (but that would require knowing every page where this editor is used). I could add it to the master layout view but this would mean overhead for the pages that don't use this editor.
So I suppose in summary all I'm asking is :- "Is there a way to include a javascript file once and only once from a EditorTemplate".
I wrote a nuget package exactly for this purpose and wrote the blog post that YD1m has referred to.
To use the package, first thing you need to do is add a call to Html.RenderScripts() somewhere in your layout so that all of the script file references and blocks added using the helpers during the rendering of a Razor view are outputted in the response. The typical place to put this call is after the core scripts in your top level layout. Here's an example layout:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>#ViewBag.Title</title>
<meta name="viewport" content="width=device-width">
</head>
<body>
#RenderBody()
<script src="~/Scripts/jquery-2.0.2.js"></script>
#* Call just after the core scripts in the top level layout *#
#Html.RenderScripts()
</body>
</html>
If you're using the Microsoft ASP.NET Web Optimization framework, you can use the overload of Html.RenderScripts() to use the Scripts.Render() method as the function for generating scripts:
#Html.RenderScripts(Scripts.Render)
With that done, now all you need to do in your editor template is use the helpers in the nuget package to add a reference to the script and add your code block
#Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { #class = "text-box single-line", type = "duration" })
#using (Html.BeginScriptContext())
{
Html.AddScriptFile("~/Scripts/maskedInput.js");
Html.AddScriptBlock(
#<script>
$(document).ready(function () {
$("##Html.NameFor(c => c)").mask("00:00:00");
});
</script>);
}
The referenced script file will only be added once to the page and all of the script blocks will be written out at the end of Html.RenderScripts() call.
Using the helpers, you can add script files and script blocks in Views, Partial Views and Editor/Display Templates. Note that the current version (1.1.0.0) will not render out scripts using the helpers when called via AJAX but this is something that I'm looking to add in the next version.
Well, you can do following:
Add #RenderSection("MaskedInput", false) to your master layout. That'l render
#section MaskedInput{}
on each page that has that section;
On a page you need to add maskedInput.js you put:
#section MaskedInput
{
#*Include scripts, styles or whatever you need here*#
}
You can create singleton class with Dictionary property whic will store scripts from your custom helpers/templates.
In your templates you can call method, that put script in singleton dictionary property with some string key. In that method you can prevent adding scripts with same keys.
Finally you should to write a render action for rendering scripts from dictionary and call this action from your master layout.
Here you can find solution similar to mine:
Managing Scripts for Razor Partial Views and Templates in ASP.NET MVC
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)