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.
@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
Share if u know something and ask if u don't. Mark as Answer if it is helpful.
Marked as answer by bojangles on Sep 20, 2011 05:45 AM
As I said I cant get a details view to work so it's in a List View, but this worked.
@Html.Raw(Item.EventDescription)
not
@Html.Raw(Model.EventDescription)
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.
bojangles
Participant
974 Points
642 Posts
MVC3 Razor String Formatting in a List View?
Sep 20, 2011 03:47 AM|LINK
Hi,
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)
@string.Format(item.EventDescription)
@item.EventDescription
This has helped to clean up my code, but can’t get the HTML formatting to work.
Cheers,
Mike.
ignatandrei
All-Star
134971 Points
21637 Posts
Moderator
MVP
Re: MVC3 Razor String Formatting in a List View?
Sep 20, 2011 03:59 AM|LINK
What do you want?
ZafarYousafi
Contributor
2542 Points
401 Posts
Re: MVC3 Razor String Formatting in a List View?
Sep 20, 2011 04:03 AM|LINK
Use HTml.Raw rather than Html.DisplayFor
ZafarYousafi
Contributor
2542 Points
401 Posts
Re: MVC3 Razor String Formatting in a List View?
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
bojangles
Participant
974 Points
642 Posts
Re: MVC3 Razor String Formatting in a List View?
Sep 20, 2011 04:12 AM|LINK
Just for the string returned from the database in the following,
@Html.DisplayFor(modelItem => item.EventDescription)
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?
Cheers,
Mike.
cyberbud
Contributor
3298 Points
551 Posts
Re: MVC3 Razor String Formatting in a List View?
Sep 20, 2011 04:48 AM|LINK
As ZafarYousafi said @Html.Raw(Model.EventDescription) is the right one for u..
MCP(.net 3.5)
Nepal
ishwor.cyberbudsonline.com
bojangles
Participant
974 Points
642 Posts
Re: MVC3 Razor String Formatting in a List View?
Sep 20, 2011 05:25 AM|LINK
Thanks Guys,
As I said I cant get a details view to work so it's in a List View, but this worked.
@Html.Raw(Item.EventDescription)
not
@Html.Raw(Model.EventDescription)
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.
Cheers,
Mike.