我已经允许HTML模型上的属性,像这样:我得到一个HttpRequestValidationException甚至当我AllowHtml的Model属性
型号
public class FooViewModel
{
[AllowHtml]
public string Description { get; set; }
}
查看
@using (Html.BeginForm("EditDescription", "Foo", FormMethod.Post))
{
@Html.AntiForgeryToken();
<input type="hidden" value="@(Model != null && Model.Item1 != null ? Model.Item1 : string.Empty)" name="fooId" />
<p>
<textarea name="Description" cols="100" id="Description">
@(Model != null && Model.Item2 != null ? Model.Item2 : string.Empty)
</textarea>
</p>
<p><input type="submit" value="Submit" /></p>
}
@section Scripts {
@Scripts.Render("~/bundles/nicEdit")
<script type="text/javascript">
bkLib.onDomLoaded(function()
{
new nicEditor({fullPanel : true}).panelInstance('Description');
});
</script>
}
控制器
public class FooController
{
public ActionResult EditDescription(string fooId)
{
if (Request.HttpMethod == "POST")
{
using (var context = new ApplicationDbContext())
{
var foo = context.Foos
.SingleOrDefault(f => f.Id == fooId);
// I get the HttpRequestValidationException here
foo.Description = Request["Description"];
context.SaveChanges();
return RedirectToAction("MyProfile");
}
}
}
}
不过,我得到一个请求验证例外。我甚至试图与ValidateInput(false)
注释整个动作的方法,因为该图只有一个领域,这是对模型的单一属性,但我仍然不断收到异常。
我清除了ASP.NET的临时文件和文件夹的缓存,清理和重建我的解决办法都无济于事。
你的基本代码有很多基础错误。你不应该使用Request.HttpMethod,而是应该使用[HttpPost和HTTPGET(http://stackoverflow.com/questions/5332275/httppost-vs-httpget-attributes-in-mvc-why-use-httppost) 。其次,你没有使用绑定来发回模型,所以你的属性几乎没用。 –
@ErikPhilips我很关注他们。我一直在使用MVC自第1版。这种特殊的作用和它的整个流程我在赶时间,与我们会回来给它解决它的客户端相互的协议后做了,现在是时候了。我在你的评论之后通过分离每种方法的操作并实际发回模型来修复它。我只是没有意识到(因为你推断)请求验证和每个HTTP请求没有单独的方法之间的关联。 –