How can I return a model with a string propertie containing <li>
elements and display it in view?
If I just write @Model.Messages it shows all the string.. i need it in html format.
Use ControllerBase. The first string represents the content of the HTML while the last is the content-type which for HTML is "text/html" . Since our controller derives from ControllerBase , we simply call base. Content() and pass the required parameter to return the desired HTML.
HtmlEncode() to send html to view and then use the Server. HtmlDecode() to get the html to display on the view. Then you can use @Html.
A controller action might return a view. However, a controller action might perform some other type of action such as redirecting you to another controller action.
The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.
You can use the Content
method with the Content-Type text/html
to return the HTML directly, without the need of Html.Raw
.
public ActionResult MyHtmlView() {
return Content("<html><body>Ahoy.</body></html>", "text/html")
}
You can pass whatever Content-Type you want, such text/xml
.
You don't say which rendering engine you're using:
MVC3:
@Html.Raw(Model.Description)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With