Asp.net mvc 显示输出时CKEditor出现故障
我正在使用CKEditor和Asp.net mvc 显示输出时CKEditor出现故障,asp.net-mvc,ckeditor,Asp.net Mvc,Ckeditor,我正在使用CKEditor和ASP.NET MVC,但有一个非常奇怪的问题:如果我格式化一些文本(例如,使用粗体、斜体或列表),当出现一些错误并且内容必须发回文本区域时,它将显示为HTML文本-“ul li”等等,而不是再次设置样式(粗体、斜体等) 我在配置中应用了一些选项: config.htmlEncodeOutput = true; // to avoid text being interpreted as attack config.enterMode = CKEDITOR.ENTER
ASP.NET MVC
,但有一个非常奇怪的问题:如果我格式化一些文本(例如,使用粗体、斜体或列表),当出现一些错误并且内容必须发回文本区域时,它将显示为HTML文本-“ul li”等等,而不是再次设置样式(粗体、斜体等)
我在配置中应用了一些选项:
config.htmlEncodeOutput = true; // to avoid text being interpreted as attack
config.enterMode = CKEDITOR.ENTER_BR; // in order not to place tags arround the text
config.basicEntities = false; // do display spaces as they are instead of and so on...
我在MVC
中尝试了一些内置函数,比如Server.HtmlDecode
和HttpUtility.Decode
,但似乎什么都不起作用。与其他编辑器一起使用的解决方案也被接受,这些编辑器可以很好地与MVC
配合使用
以下是.cshmtl文件:
@{
ViewBag.Title = "CkEditor";
}
@model HtmlTextEditorsDemos.Models.SimpleModel
<h2>CkEditor</h2>
<script src="~/Scripts/ckeditor.js"></script>
@using (Html.BeginForm())
{
@Html.TextAreaFor(x => x.Text, new { @class = "ckeditor" })
<input type="submit" value="send" />
}
SimpleModel类只是一个测试模型类,只有一个属性-Text。您可以尝试这样做
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@{
string description = Server.HtmlDecode(item.Description);
}
@Html.Raw(description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Note)
</td>
<td>
<img src="~/img/Uploads/@item.Pic" width="150" height="120" />
</td>
</tr>
}
@foreach(模型中的变量项)
{
@DisplayFor(modeleItem=>item.Title)
@{
字符串描述=Server.HtmlDecode(item.description);
}
@Html.Raw(描述)
@DisplayFor(modeleItem=>item.Note)
}
请包含如何填充该文本区域的代码。很可能您需要类似于Html.Raw
的内容。请参阅我更新的帖子。是的,Html.Raw可以完成这项工作(但仅当它显示在div或段落或其他类似元素中时),但问题是textarea中的值不能是IHtmlString,它只能是字符串值。我尝试了Html.Raw(Model.Text).ToHtmlString(),但没有成功。我也面临同样的问题,你找到了解决方法吗?
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@{
string description = Server.HtmlDecode(item.Description);
}
@Html.Raw(description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Note)
</td>
<td>
<img src="~/img/Uploads/@item.Pic" width="150" height="120" />
</td>
</tr>
}