如何进行模型验证以拾取 MVC3 中列表中对象的属性集?
我有一组与此类似的模型
public class OtherModel
{
[Required]
string name { get; set; }
}
public class OthersEditModel
{
List<OtherModel> others { get; set; }
}
,然后有一个与此类似的控制器方法。
[HttpPost]
public ActionResult EditOthers(OthersEditModel others)
{
if(ModelState.IsValid)
{
// Save
}
}
我的问题是 ModelState.IsValid
没有触发列表中对象的验证。
我该如何实现这一点,或者甚至可能吗?
或者我可以手动触发列表中元素的验证吗?
I have a set of models that looks similar to this
public class OtherModel
{
[Required]
string name { get; set; }
}
public class OthersEditModel
{
List<OtherModel> others { get; set; }
}
I then have a controller method that looks like this
[HttpPost]
public ActionResult EditOthers(OthersEditModel others)
{
if(ModelState.IsValid)
{
// Save
}
}
My problem is that the ModelState.IsValid
isn't triggering the validation of the objects in the list.
How do I accomplish this, or is it even possible?
Or alternatively can I manually trigger the validation of the elements in the list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以事实证明问题不在于验证属性。它们完美地拾取,无需对基础
OthersEditModel
进行任何操作。我破坏了动态 JavaScript 表单生成,因此表单字段返回时名称不正确。顺便说一句,这个 http://haacked. com/archive/2008/10/23/model-binding-to-a-list.aspx 是 MVC 3 的出色扩展
So it turns out that the problem wasn't the validation properties. They pick up perfectly, without doing anything to the base
OthersEditModel
. I had broken the dynamic JavaScript form generation, so the form fields were coming back with incorrect names.By the way, this http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx is an excellent extension to MVC 3