我在我的站点的管理面板上使用Html.BeginForm(),所以我用AllowHtml装饰模型属性(tinymce的目标),并在视图中使用。当我提交带有HTML字段的表单时,所有操作都很好。
但是如果我以同样的方式使用重载Html.BeginForm("action“、”控制器“),就会跳过AllowHtml并抛出众所周知的Request.form异常。我被迫使用ValidateInput(false) on Action-方法使它毫无例外地工作。你知道为什么吗?谢谢你的澄清,
这是场景/项目:Asp.net Mvc 4:
模型/ Ricetta.cs
..
[Required(ErrorMessage = "Corpo Articolo vuoto")]
[AllowHtml]
public string corpoTesto { get; set; }
..控制器/ RicetteController.cs
..
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(RicettaViewModel modelloRicetta)
{
if (ModelState.IsValid) {
..视图Ricette/Create从RicetteController中的另一个操作方法调用为视图(“创建”,modelObject)
@model WebAPP_MVC4.Areas.Admin.Models.RicettaViewModel
...
@using (Html.BeginForm("Create","Ricette",FormMethod.Post)){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
....
<fieldset>
<legend>Corpo Ricetta ~</legend>
<div class="editor-label">
@Html.LabelFor(p=>p.ricetta.corpoTesto)
</div>
<div class="editor-field">
@Html.TextAreaFor(p=>p.ricetta.corpoTesto, new { @cols = 60, @rows = 20})
@Html.ValidationMessageFor(p=>p.ricetta.corpoTesto)
</div>
</fieldset>
..发布于 2013-06-08 21:03:03
我做了快速测试,一切都很完美-- Html.BeginForm()和Html.BeginForm("action“、”控制器“)之间的行为没有区别。也许这个问题的原因是在源代码中,你没有向我们展示。
下面是我的代码(工作):
VieModel:
public class PostViewModel
{
[AllowHtml]
[Required]
public string Content { get; set; }
}主计长:
public ActionResult Index()
{
return View("Create", new PostViewModel());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(PostViewModel model)
{
if (ModelState.IsValid)
{
return Index();
}
return View(model);
}查看:
@model SendHTmlTpControler.Models.PostViewModel
<html>
<head>
<script src="~/Scripts/tinymce/tiny_mce.js"></script>
<script type="text/javascript">
tinymce.init({
selector: "textarea",
toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});
</script>
</head>
<body>
<h2>Create</h2>
@using (Html.BeginForm("Create", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Content)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Content, new { @cols = 60, @rows = 20 })
@Html.ValidationMessageFor(model => model.Content)
</div>
<p>
<input type="submit" value="Save" />
</p>
}
</body>
</html>https://stackoverflow.com/questions/16995835
复制相似问题