MVC 3 RTM。拥有一个具有AllowHtml 属性的模型。在我的控制器操作中,如果该操作将 FormCollection 作为参数,则会引发异常:
[HttpPost]
public ActionResult Edit(FormCollection collection, int id)
{
var myEntity = _myRepo.Get(id);
TryUpdateModel(myEntity);
return DoSave(myEntity);
}
潜在危险的 Request.Form
从客户端检测到值
但是,如果我的控制器操作使用对象而不是 FormCollection,它不会引发异常。
[HttpPost]
public ActionResult Edit(MyEntity postedEntity, int id)
{
var myEntity = _myRepo.Get(id);
TryUpdateModel(myEntity);
return DoSave(myEntity);
}
我已经设置好了
http运行时
请求验证模式=“2.0”
为什么使用 FormCollection 时会失败?
你不能使用AllowHtml
with FormCollection
。你可以使用[ValidateInput]属性,但显然这禁用了所有值的验证:
[HttpPost]
[ValidateInput(false)]
public ActionResult Edit(FormCollection collection, int id)
{
var myEntity = _myRepo.Get(id);
TryUpdateModel(objective);
return DoSave(objective);
}
话虽如此,我将使用以下内容:
[HttpPost]
public ActionResult Edit(MyEntity entity)
{
if (ModelState.IsValid)
{
_myRepo.Save(entity);
return RedirectToAction("Success");
}
return View(entity);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)