为什么我不能在 ASP.NET MVC 3 中进行 HtmlDecode

发布于 2024-10-19 19:15:00 字数 2579 浏览 2 评论 0原文

这是场景。我想使用 CKEditor 作为表单上的富文本字段,但无论出于何种原因,我无法从textarea 到服务器并返回页面,没有编码问题。这是我编写的一个小示例程序,旨在尝试弄清楚发生了什么。首先,我的视图模型:

HomeViewModel.cs

namespace CkEditorTest.Models
{
    public class HomeViewModel
    {
        [Required]
        [DataType(DataType.Html)]
        [Display(Name = "Note")]
        public string Note { get; set; }
    }
}

现在我的控制器:

HomeController.cs

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

@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 编码的(即

等...)。以下是发布前的表单:

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

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

发布结果 http://www.matthewkimber.com/images/so/posted.png

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

断点结果 http://www.matthewkimber.com/images/so/dataInsideTheActionMethod.png< /a>

当然,这是精简测试。我尝试对其进行编码,在视图和控制器中对其进行解码,但均无济于事。

Here is the scenario. I want to use CKEditor for a rich text field on a form, but for whatever reason I cannot get the contents from the textarea to the server and back to the page without encoding problems. Here is the little sample program I wrote up to try and figure out what is going on. First, my view model:

HomeViewModel.cs

namespace CkEditorTest.Models
{
    public class HomeViewModel
    {
        [Required]
        [DataType(DataType.Html)]
        [Display(Name = "Note")]
        public string Note { get; set; }
    }
}

Now my controller:

HomeController.cs

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);
        }
    }
}

And finally, my view:

Index.cshtml

@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>
}

No matter what I do, I cannot display the Model.Note property as html on my view. By the time it reaches the view it is HTML encoded (i.e. <p> etc...). Here is what the form looks like pre-post:

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

And here is what the result is in the div below the "Submit" button:

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

I've set a breakpoint within Visual Studio and it shows as bare angle brackets (no encoding on HTML elements, just characters).

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

This, of course, is the stripped down test. I've tried encoding it, decoding it both in the view and in the controller to no avail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单 2024-10-26 19:15:00

默认情况下,当您使用 razor 时,所有内容都会被编码。我认为您正在寻找 原始方法

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

By default everything is encoded when you use razor. I think you're looking for the Raw method.

It would also be a good idea to check the response using Fiddler or Firebug.

落花浅忆 2024-10-26 19:15:00

试试这个:

@Html.DisplayTextFor(modelItem => item.Note)

Try this:

@Html.DisplayTextFor(modelItem => item.Note)
指尖凝香 2024-10-26 19:15:00

您还可以使用 HtmlString("")

You can also use HtmlString("")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文