Mat*_*teo 7 asp.net-mvc ckeditor
这是场景.我想将CKEditor用于表单上的富文本字段,但无论出于何种原因,我无法将内容从textarea获取到服务器并返回到页面而不会出现编码问题.这是我编写的小样本程序,试图弄清楚发生了什么.首先,我的视图模型:
HomeViewModel.cs
namespace CkEditorTest.Models
{
    public class HomeViewModel
    {
        [Required]
        [DataType(DataType.Html)]
        [Display(Name = "Note")]
        public string Note { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)
现在我的控制器:
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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)
最后,我的观点是:
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>
}
Run Code Online (Sandbox Code Playgroud)
无论我做什么,我都无法在我的视图中将Model.Note属性显示为html.到达视图时,它是HTML编码的(即<p>等...).这是表格在帖子前的样子:
前期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
当然,这是精简测试.我试过编码它,在视图和控制器中解码它无济于事.
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           9440 次  |  
        
|   最近记录:  |