我是一名有帮助的助手,将为您进行翻译。
我正在尝试使用一个简单的表格来允许授权用户修改我正在构建的MVC3 Razor网站上选择页面的内容。然而,我无法正确提交编辑表单。
我的模型如下:
public class WebContent
{
public virtual UInt32 id { get; set; }
public virtual String page { get; set; }
public virtual String section { get; set; }
[UIHint("tinymce_jquery_full"), AllowHtml]
public virtual String content { get; set; }
}
我的控制器:
[Authorize]
public ActionResult Edit(String page, String section)
{
WebContent content = _WebContent.GetSection(page,section);
return View(content);
}
[Authorize]
[HttpPost]
public ActionResult Edit(WebContent content)
{
if (ModelState.IsValid)
{
_WebContent.Update(content);
return View("Index");
}
else return View("Index");
}
我的视图:
@model SongbirdsStudios.Models.WebContent
@{
ViewBag.Title = "Edit '"+Model.page+"'Page Content";
}
<div>
<h2>Edit</h2>
@using (Html.BeginForm())
{
<fieldset>
<legend>Page Content</legend>
@Html.HiddenFor(m => m.id)
@Html.HiddenFor(m => m.page)
@Html.HiddenFor(m => m.section)
<div class="editor-label">
Content
</div>
<div class="editor-field">
@Html.EditorFor(m => m.content)
</div>
<p>
<input type="submit" value="Update" />
</p>
</fieldset>
}
</div>
视图正确渲染并显示了预期的元素。UIHint("tinymce_jquery_full")被正确识别,TinyMCE编辑器出现在页面上。但是,当表单提交时,我遇到了异常。
System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (content=...)
根据我阅读的所有内容,AllowHTML属性应该允许提交此内容,但由于某种原因它没有生效。
我可以通过在HttpPost控制器方法中添加[ValidateInput(false)]属性来解决这个问题。如果我这样做,那么就不会发生此异常,但是模型仍然无法传递到控制器。它只会传递null。在调试器中检查HttpContext表明,它会传递4个单独的值——每个模型属性一个,而不是将整个模型类返回给控制器。我无法找出需要更改的内容以使其正常工作。
我希望这是我忽略的一些简单问题,期待有更好的眼力看到它是什么。