简体   繁体   English

如何在@ Html.TextBox asp .Net MVC中呈现html代码?

[英]How to render html code in @Html.TextBox asp .Net MVC?

Hi I'm creating MVC application and I'm using tinyMCE. 嗨,我正在创建MVC应用程序,并且正在使用tinyMCE。 Sometimes i need to change tinyMCE with simple TextBox field. 有时我需要使用简单的TextBox字段更改tinyMCE。 I'm trying this with hide and show. 我正在尝试用捉迷藏的方式。 At the begging I'm showing TextArea and hidding TextBox, but when user click convert tinyMCE to simple textBox field, I'm hidding textArea and showing TextBox. 最初,我正在显示TextArea并隐藏TextBox,但是当用户单击“将tinyMCE转换为简单textBox字段”时,我正在隐藏textArea并显示TextBox。 This is my tinyMCE field 这是我的tinyMCE领域

  @Html.TextAreaFor(model => model.supTitle, new { htmlAttributes = new { @class = "form-control" } })

and this is simple TextBox 这是简单的TextBox

 @Html.TextBoxFor(model => model.supTitle, new { htmlAttributes = new { @class = "form-control" } })

but when I'm using TextBoxFor I have something like this 但是当我使用TextBoxFor时,我有这样的东西

<p> some text</p>

but I want only some text to be displayed without <p> tags. 但我只希望显示some text没有<p>标记的some text Also when I save the changes only the changes from TextArea are saves into database. 另外,当我保存更改时,只有来自TextArea的更改才保存到数据库中。

If the value stored in the model.supTitle property contains Html as a string you would have to parse out the html before rendering the value to a standard input[type=text] . 如果存储在model.supTitle属性中的值包含Html作为字符串,则必须在将值呈现为标准input[type=text]之前解析出html。

Only the first value is persisted to the database because both form elements have the same name, when the form is posted both values will be sent in the Http Post but the MVC model binding will only map the first form value to the model (which in this case is the text area). 因为两个表单元素具有相同的名称,所以只有第一个值会保留在数据库中,当发布表单时,两个值都将在Http Post中发送,但是MVC模型绑定只会将第一个表单值映射到模型(这种情况是文本区域)。

Hiding & showing a html element does not prevent the value of the element from being posted in a form post, you would need to either remove the element from the DOM altogether or change the element name so that it is not bound by the model binder. 隐藏和显示html元素不会阻止该元素的值在表单发布中发布,您将需要从DOM中完全删除该元素或更改元素名称,以使其不受模型绑定器的约束。

尝试在您的视图中使用它

@Html.Raw(Model.YourTinyMCEContent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM