I've searched over this website and I've looked over similar questions and i did not find the answer, I am sure it is somewhere but i did not find it, I have a string like this one for example :
string inputText = "<strong>Hello World</strong>"
This string comes from a certain request in control, and i have no power to change the model or the control. I can only change my razor view, using Html.Raw displays this result :
<strong>Hello World</strong>
And the result i want to be displayed is this one :
Hello World
How is it possible ?
PS: this is only a simple example, it can be any HTML Code.
You should use:
#Html.Raw(HttpUtility.HtmlDecode(inputText))
Decode and then render in html
To render any string (which includes HTML tags) -received from the model- as HTML, use:
#Html.Raw(Model.SomeString)
Related
I have an HTML string that also contains a call to a partial view
When I render the string as such, it renders the HTML but not the Partial View.
My Partial file has the text Welcome to my Partial!
#Model.MyText = "This is my Text #Html.Partial('Partials/Partial') Done!";
#Html.Raw(Model.MyText)
Renders:
This is my Text #Html.Partial('Partials/Partial') Done!
Instead of:
This is my Text. Welcome to my Partial! Done!
How would I achieve this?
The razor snippet only works in the context of the .csharp file itself, not the rendered webpage. When you call #Html.Raw(Model.MyText), it correctly renders the string as html. However the resulting html is not parsed as C# code, it is just a string of text. This is not the way to achieve your desired result.
Everything you need done using C# must be done before the render. After rendering, its just html code/text.
I have an ASP.NET MVC3 web page which used to nicely display a C# string of HTML nicely. My server code programmatically loads the string with HTML tags for a nice table. The following cshtml has been showing this nice table as a nice table for years:
<h3>Nice Table:</h3>
#Html.DisplayTextFor( model => model.sNiceTable )
But today we notice browsers (Firefox & IE) are instead displaying it as a pile of text showing the HTML tag text:
<table><tr><th>...
rather than rendering it visually as a table, the way it did for years until now. The "Nice Table" header still displays properly.
Looking at the text in Firefox's developer Inspector shows HTML that looks like it should render as HTML, but (after showing the <h3> tag in color) it too has those tags from my string in black, even though when I paste the same string into an HTML file and load it, it shows the nice table. That is, Inspector shows the code as if it should work, but my string is in black for some reason that doesn't show in its code view.
Trying to use "view source" in Firefox or IE seems to be unusable for this, because the view part doesn't even show up there for some reason.
I see there are various other methods such as Display() and DisplayFor(), but they seem to do the same thing.
I haven't really touched this page since it last worked. The only thing I suspect that changed was applying some MS updates that had some other annoying side-effects on my projects.
What can I do to get this text to render again, rather than spew the HTML tags in the string as text?
Edit: model.sNiceTable is a C# string data member of the viewmodel, where the interface is:
public interface IMemberStatsViewModel
{
string sNiceTable { get; set; }
// other properties...
}
Its value gets assembled in my code which is called in the view model's constructor:
public MemberStatsViewModel()
: base(new DataLayer.DataLayer(), ClientInterfaceTypeEnum.Web)
{
InitializeMe();
}
InitializeMe() calls a data layer function which returns the C# string and assigns it to sNiceTable. The string is built based on stuff I look up in the data layer.
You can use Html.Raw
<h3>Nice Table:</h3>
#Html.Raw(model.sNiceTable)
I am in a process of upgrading a c# MVC2 project into c# MVC4.
Here is the scenario in MVC2
Input string(from database)
Model.text="<p>Hi<br>hello!<br>you there</p>"
Output (rendered in the view)
rendered using
<%=Model.text %>
Hi
hello!
you there
Here is the scenario in MVC4
Input string(from database)
Model.text="<p>Hi<br>hello!<br>you there</p>"
Output (rendered in the view)
rendered using
#Model.text
<p>Hi<br>hello!<br>you there</p>
I tried
#HttpUtility.HtmlDecode(Model.text)
#HttpUtility.HtmlEncode(Model.text)
Nothing helps...
I had a similar problem in MVC4 asked here (the ajax result is rendered with html tags not the actual html)
Is some of my settings troubling me??? or is that something to do with HTML 5 or am I missing anything in using MVC4.
Please help!!
This should do the trick:
#Html.Raw(Model.text)
If you don't want your text get encoded, that text should be of type IHtmlString. String texts are encoded by default.
In your case,
Model.text = MvcHtmlString.Create("<p>Hi<br>hello!<br>you there</p>");
would do the trick as well.
In controller side
viewbag.msg="hello";
in the html.cs razor view
#Html.Raw(viewbag.msg)
Consider the following example:
<div>#ViewBag.MyData</div>
What to do if ViewBag.MyData contains a string with "\n" (newlines) in it, that I want to change into "<br/>"? When I include "<br/>" instead of the newlines, it gets encoded in the browser, which is bad.
How can I help this?
use #Html.Raw(ViewBag.MyData.Replace("\n", "</br>"))
the following will do what you need...
#MvcHtmlString.Create(ViewBag.MyData.Replace("\n", "</br>"))
The Create method will take a HTML string and render it as would be expected if you just typed the HTML straight onto the page
That's what html pre tag is used for
The < pre> tag defines preformatted text. Text in a < pre> element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks ...
And in razor template
< pre>#ViewBag.MyData</ pre>
I have tried to use the standard AJAX HTMLeditor from here (http://www.asp.net/ajaxlibrary/act.ashx) and I have try to work with the FCKEditor (from http://ckeditor.com/)
But both don't do everything. I call the AJAX standard control A and the FCKeditor F.
With the A editor it is impossible to get your HTML text in the HTML content. You can only get it in the Design content. (this next code doesn't do the job: string htmlContentStr = Editor1.Content).
With F it is possible to get it in the HTML content (it does this by default), but to get your changes back in HTML is impossible. (this next code doesn't do the job: string htmlContentStr = FCKeditor1.Value).
So what I need is a HTML editor that is possible to put HTML text in HTML content, a user can make changes in the designcontent and after the changes 're make it must be possible to get the HTMLcontent and put it away in a string or database.
Is this possible or do I need a commercial one to get this feature?
If my question isn't clear, please let me know.
Thnx
I've used XStandard quite easily and it let me manipulate the HTML. I didn't bother using it as a control, but just read and wrote (escaped) the HTML where needed into the asp output.