Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Sep 20, 2011 05:25 AM by bojangles
Sep 20, 2011 03:47 AM|LINK
I may be doing this incorrectly, I can’t get a Details View to work at all but it all works fine if I use a list view, but not the formatting.
I have: @Html.DisplayFor(modelItem => item.EventDescription) It is an HTML formatted string coming from the database, but displays as a string, i.e..no formatting.
I’m coming in from a web forms background and thanks to some brilliant MVC, MVP’s earlier today I’ve made progress in the area and found I’m able to so the same things with the following, even though MVC gives me the first example by default.
@Html.DisplayFor(modelItem => item.EventDescription)
This has helped to clean up my code, but can’t get the HTML formatting to work.
Sep 20, 2011 03:59 AM|LINK
can’t get the HTML formatting to work.
What do you want?
Sep 20, 2011 04:03 AM|LINK
Use HTml.Raw rather than Html.DisplayFor
Html.DisplayFor returns IHtmlString of encoded data whereas Raw returns IHtmlString of the data as it is.
Sep 20, 2011 04:06 AM|LINK
@String.Format and @Item.EventDescription will not work because razor by default encode the plain string before sending it to the output
in both the cases razor is getting plain text where Html.Display and HTml.Raw returns IHtmlString for which razor sends The text as it is to the output.
However in case of Html.DisplayFor, this helper method itself encodes the html and thats why u r not receiving desired output
Sep 20, 2011 04:12 AM|LINK
Just for the string returned from the database in the following,
To format on the screen as HTML, i.e.. the <p> and <strong> tags in the sting to do their thing.
Currently it displays as,
<p>This is a new line</p><p>and this is another line</p>
But should display like:
This is a new line
and this is another line
It is in a list view though, is that a problem?
Sep 20, 2011 04:48 AM|LINK
As ZafarYousafi said @Html.Raw(Model.EventDescription) is the right one for u..
Sep 20, 2011 05:25 AM|LINK
As I said I cant get a details view to work so it's in a List View, but this worked.
I'm not sure why a details view doesn’t work, I had the same problem with web forms. I only used a details view if I absolutely had to e.g.. while using a CRUD set up.