开发者

Rendering html text from model inside view

开发者 https://www.devze.com 2023-03-17 17:52 出处:网络
In my mvc application, I have a view in which the content for the view will have to be rendered in html format. html string will be obtained form the model in the view. How can i rener the html mark u

In my mvc application, I have a view in which the content for the view will have to be rendered in html format. html string will be obtained form the model in the view. How can i rener the html mark up as such in view?

开发者_运维技巧Thanks in advance


Have a read through: http://www.asp.net/mvc/tutorials/asp-net-mvc-views-overview-cs to see an example of the ASPX view engine rendering HTML.

You shouldn't be building HTML in the model (or in the controller and putting it into the model).

Update: as Jamie Dixon has mentioned, there are situations in which business properties of the model will contain HTML.

Generally, speaking:
The model should contain properties and business logic.
The views will render the HTML that you need.
The controller calls the business logic needed and passes the model to the correct view.

If, in the view, you wish to write out a heading based on a property called Title on the Model, you can use this:

<h1><%: Model.Title %></h1>

or if you are storing a HTML string in a property called StoredHtml:

<div>
    <%= Model.StoredHtml%>
</div>

Note that <%= has been used rather than <%:, as <%: will encode HTML for security reasons (like <script> tags being saved).

0

精彩评论

暂无评论...
验证码 换一张
取 消