如何在@ Html.TextAreaFor()中使用@ Html.Raw() [英] How to use @Html.Raw() in @Html.TextAreaFor()
问题描述
我正在使用summernote添加图像,视频,文本.另外,我将图像或视频另存为Html Code,并将其键入字符串到数据库中.当我从数据库中检索视频或图像以显示在summernote上时,大约需要5分钟,我不知道为什么.但是,当我从数据库中检索文本以显示在summernote上时,没有问题.
I am using summernote to add Image, Video, text. Also, I save image or video as Html Code that is type string to database. When I retreive video or image from the database to display on summernote, it takes nearly 5 minutes and I don't know why. However, when I retrieve text from the database to display on summernote, there is no problem.
这是模特
public class Article
{
[AllowHtml]
[Column(TypeName = "varchar(MAX)")]
public string Content { get; set; }
}
这是Summernote编辑器的视图
Here is View for summernote editor
@Html.TextAreaFor(x => Model.Content, new { @class = "", placeholder = @Blog.Helper.Resources.Content, @id = "summernote_1" })
此外,如果我在View中使用以下代码
In addition, If I use this below code in View
<div id="summernote_1">@Html.Raw(Model.Content)</div>
显示视频或图像没有问题,但是我无法编辑summernote.当我编辑summernote时,Model.Content为空.
No problem with displaying video or image, but I cannot edit summernote. When I edit summernote, Model.Content is getting null.
如果我在View中使用以下代码
If I use this below code in View
@Html.TextAreaFor(x => Model.Content, new { @class = "", placeholder = @Blog.Helper.Resources.Content, @id = "summernote_1" })
编辑Summernote没问题,但是显示视频或图像大约需要5分钟.
No problem with editing summernote, but displaying video or image takes nearly 5 minute.
由于这些原因,我试图在View中使用下面的代码,但是下面的代码出错.
Because of these reasons, I am trying to use this below code in View however this below code is getting error.
@Html.TextAreaFor(x => Html.Raw(Model.Content), new { @class = "", placeholder = @Blog.Helper.Resources.Content, @id = "summernote_1" })
我可以在html.TextAreaFor中使用html.raw还是该问题的解决方法,以显示视频或图像并编辑summernote. 对不起,我的英语不好,我希望有人能帮助我.
Can I use html.raw in html.TextAreaFor or What can I do for this issue to display video or image and edit summernote. Sorry my bad english and I hope that someone can help me.
推荐答案
Html.Raw
方法返回System.Web.IHtmlString
,我认为您所需要的只是传递包含HTML标记的string
属性(AFAIK Html.Raw
帮助器不能可以在其他HTML帮助器(如DisplayFor
或TextBoxFor
)中使用.
Html.Raw
method returns System.Web.IHtmlString
, I think what you need is just passing string
property containing HTML tags (AFAIK Html.Raw
helper can't be used inside other HTML helpers like DisplayFor
or TextBoxFor
).
如本示例所示,可以在TextAreaFor
之前对Content
属性使用HttpUtility.HtmlDecode
(尤其是如果HTML标记在不知不觉中被编码的话):
It is possible to use HttpUtility.HtmlDecode
for Content
property before TextAreaFor
as shown by this example (especially if HTML tags are unknowingly encoded):
@model Article
@{
Model.Content = HttpUtility.HtmlDecode(Model.Content);
}
@Html.TextAreaFor(x => x.Content, new { @class = "", placeholder = @Blog.Helper.Resources.Content, @id = "summernote_1" })
通过这种方式,您可以从viewmodel的string属性正确渲染HTML标签,而不会丢失模型绑定.
By using this way, you can render HTML tags properly from viewmodel's string property without losing model binding.
这篇关于如何在@ Html.TextAreaFor()中使用@ Html.Raw()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!