如何获得模型验证,以在MVC3列表设置对象的属性皮卡? [英] How to get model validation to pickup attributes set on objects in a List in MVC3?

查看:168
本文介绍了如何获得模型验证,以在MVC3列表设置对象的属性皮卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组模型,类似于此

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
    }
}

我的问题是,在列表中的对象的 ModelState.IsValid 不触发验证。

我如何做到这一点,或者是它甚至可能吗?

How do I accomplish this, or is it even possible?

或者替换地可以我手动触发的元素列表中的验证?

Or alternatively can I manually trigger the validation of the elements in the list?

推荐答案

因此​​,原来的问题不在于验证属性。他们拿起完美的,没有做任何事情的基础 OthersEditModel 。我打破了动态JavaScript形式产生,使表单字段,用不正确的名称回来了。

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.

顺便说一句,这的http:/ /haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx 是一个极好的扩展MVC 3

By the way, this http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx is an excellent extension to MVC 3

这篇关于如何获得模型验证,以在MVC3列表设置对象的属性皮卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆