【发布时间】:2023-03-07 07:54:01
【问题描述】:
我正在处理一个 ASP.NET MVC 3 项目,需要从服务器端验证中排除某些属性。我正在寻找类似这个问题Is it possible to override the required attribute on a property in a model? 的内容,但我不想再次绑定模型,因为我已经对其进行了更改(据我了解,这就是 TryUpdateModel 会做的)。
来自相关问题
public ActionResult SomeAction()
{
var model = new BaseModel();
if (TryUpdateModel(model, null, null, new[] { "RequiredProperty" })) // fourth parameter is an array of properties (by name) that are excluded
{
// updated and validated correctly!
return View(model);
}
// failed validation
return View(model);
}
我想验证已更新的模型,不包括某些属性。我应该只使用链接问题中建议的 hackedbychinese 之类的 TryUpdateModel 吗?它可以改变属性的值吗?
【问题讨论】: