在工作中扫描应用程序时,很明显,如果您尝试将该模型传递回视图,则修改输入值,然后发布它可能会导致渲染问题。 例如,使用模型:
public class Survey
{
public bool LikesCandy { get; set; }
}
行动:
public ActionResult Survey()
{
return View(new Survey());
}
[HttpPost]
public ActionResult Survey(Survey model)
{
//do something worthwhile with the model, like validation, etc. etc.
//even setting the model's boolean doesn't help the rendering.
model.LikesCandy = true;
//then return to the page
return View(model);
}
一个基本的剃刀观点:
@model BoolTest.Models.Survey
@using (Html.BeginForm())
{
@Html.EditorFor(i => i.LikesCandy)
<input type="submit" value="Submit"/>
}
表单呈现如下:
<form action="/Home/Survey" method="post">
<input checked="checked" id="LikesCandy" name="LikesCandy" type="checkbox" value="true">
<input name="LikesCandy" type="hidden" value="false">
<input type="submit" value="Submit">
</form>
发布工作正常。
但是,如果有人像这样更改输入值:
<form action="/Home/Survey" method="post">
<input checked="checked" id="LikesCandy" name="LikesCandy" type="checkbox" value="foobar">
<input name="LikesCandy" type="hidden" value="false">
<input type="submit" value="Submit">
</form>
模型在服务器端看起来合理(默认LikesCandy
为false),但视图呈现将始终以String was not recognized as a valid Boolean.
失败。
有人可以解释为什么会这样吗?是否有一些惯用的方法来修复/处理这个?我可以做一些事情,比如使用反射将模型的属性类型与请求表单进行比较,并给用户一些“停止!”消息,或者编写我自己的html标签并手动将模型绑定到那些,但看起来都不是很干净/可扩展/正确。
答案 0 :(得分:1)
编辑:正如评论中所述,模型绑定器期望是真或假。如果您为该字段提交了其他内容,那么500错误就是预期的响应。此外,如果您在后期处理期间更改模型的值,然后重新显示视图,那么您将不会看到在POST响应中反映的控制器中所做的模型更改。见这里:
http://patrickdesjardins.com/blog/modelstate-clear-is-required-to-display-back-your-model-object
public ActionResult Survey()
{
return View(new Survey());
}
[HttpPost]
public ActionResult Survey(Survey model)
{
//do something worthwhile with the model, like validation, etc. etc.
//even setting the model's boolean doesn't help the rendering.
model.LikesCandy = true;
//clear model state so that the change on the line above is applied
ModelState.Clear();
//then return to the page
return View(model);
}