store and display html tags in MVC

China☆狼群 提交于 2019-12-02 06:39:57

问题


How can I store post.content as html in database and how can I display with rendered html without tags. I am trying with following way, but it's not working. It can stored encode html in database but its not displayed rendered html. Any best practice would be appreciated.

1) //Saving post content in database as html

public ActionResult Edit(Post post, FormCollection obj)
        {               
          post.Content = Server.HtmlEncode(post.Content);
        }

2) //Displaying post content to view

<%: System.Web.HttpUtility.HtmlDecode(item.Content)%>

OR

<%: item.Content%>

回答1:


MVC3/Razor:

@Html.Raw(item.Content)

MVC2/WebForms:

<%: MvcHtmlString.Create(item.Content) %>


来源:https://stackoverflow.com/questions/5202218/store-and-display-html-tags-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!