3

Possible Duplicate:
ASP.NET MVC Razor render without encoding

One of the proerties of my Product object is returning a string that has html in it. When I set the model to the view, the view reads it as text and not html. How do I let the view know to read the property as html?

<td colspan="2">
 <div>
   <label >                             
       @Model.LongDescription  
   </label>                                 
 </div>
</td>
4

3 回答 3

9

Razor encodes everything by default, you just have to use @Html.Raw see this question which is pretty much the same problem you have

<td colspan="2">
 <div>
   <label >                             
       Html.Raw(@Model.LongDescription)
   </label>                                 
 </div>
</td>
于 2012-07-13T13:49:05.483 回答
0

Use Html.Raw helper method to ignore encoding

<label >                             
   @Html.Raw(Model.LongDescription)  
</label>  
于 2012-07-13T13:50:53.930 回答
0

You need to use the HtmlString object. I prefer to handle this in my model by adding a property to the model:

public HtmlString LongDescription
{
    get
    {
        return new HtmlString(LongDescription);
    }
}

The object HtmlString will leave the html formatting in place for display.

于 2012-07-13T13:51:00.203 回答