asp.net-mvc – 为什么我不能在ASP.NET MVC 3中使用HtmlDecode

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 为什么我不能在ASP.NET MVC 3中使用HtmlDecode前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是场景.我想在表单上使用 CKEditor作为富文本字段,但无论出于何种原因,我无法将内容从textarea获取到服务器并返回到页面而不会出现编码问题.这是我编写的小样本程序,试图弄清楚发生了什么.首先,我的视图模型:

Homeviewmodel.cs

@H_404_4@namespace CkEditorTest.Models { public class Homeviewmodel { [required] [DataType(DataType.Html)] [Display(Name = "Note")] public string Note { get; set; } } }

现在我的控制器:

HomeController.cs

@H_404_4@using System.Web.Mvc; using CkEditorTest.Models; namespace CkEditorTest.Controllers { public class HomeController : Controller { public ActionResult Index() { return View(new Homeviewmodel()); } [HttpPost] [ValidateInput(false)] public ActionResult Index(Homeviewmodel model) { return View(model); } } }

最后,我的看法:

Index.cshtml

@H_404_4@@model CkEditorTest.Models.Homeviewmodel @{ ViewBag.Title = "CKEditor Test"; } @section head { <script type="text/javascript" src="@Url.Content("~/Scripts/ckeditor/ckeditor.js")"></script> <script type="text/javascript" src="@Url.Content("~/Scripts/ckeditor/adapters/jquery.js")"></script> <script type="text/javascript"> $(document).ready(function () { $("#Note").ckeditor(); }); </script> } <h2>CKEditor Test</h2> @using (Html.BeginForm()) { @Html.LabelFor(m => m.Note)<br /><br /> @Html.TextAreaFor(m => m.Note)<br /> <input type="submit" /> } @if (!String.IsNullOrEmpty(Model.Note)) { <div id="noteText">@Model.Note</div> }

无论我做什么,我都无法在我的视图中将Model.Note属性显示为html.到达视图时,它是HTML编码的(即< p>等……).这是表格在帖子前的样子:

pre-post http://www.matthewkimber.com/images/so/pre-post.png

以下是“提交”按钮下方div中的结果:

post result http://www.matthewkimber.com/images/so/posted.png

我在Visual Studio中设置了一个断点,它显示为裸角括号(HTML元素上没有编码,只是字符).

breakpoint results http://www.matthewkimber.com/images/so/dataInsideTheActionMethod.png

当然,这是精简测试.我试过编码它,在视图和控制器中解码它无济于事.非常感谢您的帮助!谢谢!

解决方法

默认情况下,当您使用剃刀时,所有内容都会被编码.我想你正在寻找 Raw method.

使用Fiddler或Firebug检查响应也是一个好主意.

猜你在找的asp.Net相关文章