ASP.NET MVC的复杂模型验证 [英] ASP.NET MVC Complex Model Validation

查看:114
本文介绍了ASP.NET MVC的复杂模型验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的模型

 公共类ComplexModel
{    公众的usermodel的usermodel;    公共ExtraInfoModel extraModel;
}

其中,

的usermodel可能必填字段

 公共类的usermodel
{     [需要]
     公共字符串的电子邮件;
}

我怎么做验证ComplexModel,以确保其成员模型数据说明正在采取入帐户ComplexModel验证?

感谢您。

更新:

下面是我确切的情况。当我在ManageProfileModel的控制器操作,无论GeneralInfoModel的ListModelRequired成员的SelectedValue是否设置或不叫ModelState.IsValid模型的状态是有效的。

 公共类ManageProfileModel
{
    [必需(的ErrorMessage =经验要求)]
    公众诠释LevelOfExperienceTypeID {搞定;组; }
    公共GeneralInfoModel GeneralInfoModel {搞定;组; }
}公共类GeneralInfoModel
{
    [需要]
    [显示名称(资料标题)]
    公共字符串配置文件名{获得;组; }    [显示名称(签名)]
    公共特征码{搞定;组; }    [需要]
    公共ListModelRequired LevelOfExperience {搞定;组; }
}公共类的ListModel
{
    公共ListModel的()
    {
    }    公众的ListModel(字符串名称)
    {
        this.Name =名称;
    }    公众的ListModel(字符串名称,字符串了selectedValue):这个(名)
    {
        this.SelectedValue =了selectedValue;
    }    公众的ListModel(字符串名称,IEnumerable的< SelectListItem>成员):这(名)
    {
        this.Members =成员;
    }    公众的ListModel(字符串名称,IEnumerable的< SelectListItem>成员串了selectedValue)
        :这个(姓名,成员)
    {
        this.SelectedValue =了selectedValue;
    }    公共IEnumerable的< SelectListItem>成员{搞定;组; }    公共字符串名称{;组; }    公共虚拟字符串的SelectedValue {搞定;组; }    公共字符串标签{搞定;组; }
}公共类ListModelRequired:ListModel的
{
    [需要]
    公共重写字符串的SelectedValue {搞定;组; }    公共ListModelRequired():基地()
    {
    }    公共ListModelRequired(字符串名称):基地(名)
    {
    }    公共ListModelRequired(字符串名称,字符串了selectedValue):基地(姓名,了selectedValue)
    {
    }    公共ListModelRequired(字符串名称,IEnumerable的< SelectListItem>成员)
        :基地(姓名,成员)
    {
    }    公共ListModelRequired(字符串名称,IEnumerable的< SelectListItem>成员串了selectedValue)
        :基地(名称,成员了selectedValue)
    {
    }
}


解决方案

我觉得默认的模型绑定得到这一权利,验证使用数据的注释子模型。问题是什么,你实际上它有它?例如,我有类似的东西:

 公共类OnlineDonationModel
{
     [需要]
     公共小数? {金额获得;组; }     公共ContactModel联系{搞定;组; }
}公共类ContactModel
{
     [需要]
     公共字符串名字{获得;组; }     [需要]
     公共字符串名字{获得;组; }     [需要]
     公共字符串地址{搞定;组; }     ...
}

I have a complex Model

public class ComplexModel
{

    public UserModel userModel;

    public ExtraInfoModel extraModel;
}

where

UserModel may have required fields

as in:

public class UserModel
{

     [Required]
     public string email;
}

how do I validate ComplexModel to make sure that the data annotations on its member models are being taken into the account in ComplexModel validation?

Thank you.

UPDATE:

Here's my exact scenario. When I call ModelState.IsValid in a controller action on ManageProfileModel regardless of whether "SelectedValue" of ListModelRequired member of GeneralInfoModel was set or not the model state is valid.

public class ManageProfileModel
{
    [Required(ErrorMessage="Experience is required")]
    public int LevelOfExperienceTypeID { get; set; }
    public GeneralInfoModel GeneralInfoModel { get; set; }
}

public class GeneralInfoModel
{
    [Required]
    [DisplayName("Profile Headline")]
    public string ProfileName { get; set; }

    [DisplayName("Signature")]
    public string Signature { get; set; }

    [Required]
    public ListModelRequired LevelOfExperience { get; set; }
}

public class ListModel
{
    public ListModel()
    {
    }

    public ListModel(string name)
    {
        this.Name = name;
    }

    public ListModel(string name, string selectedValue):this(name)
    {
        this.SelectedValue = selectedValue;
    }

    public ListModel(string name, IEnumerable<SelectListItem> members):this(name)
    {
        this.Members = members;
    }

    public ListModel(string name, IEnumerable<SelectListItem> members, string selectedValue)
        : this(name, members)
    {
        this.SelectedValue = selectedValue;
    }

    public IEnumerable<SelectListItem> Members { get; set; }

    public string Name { get; set; }

    public virtual string SelectedValue { get; set; }

    public string Label { get; set; }
}

public class ListModelRequired : ListModel
{
    [Required]
    public override string SelectedValue { get; set; }

    public ListModelRequired():base()
    {
    }

    public ListModelRequired(string name):base(name)
    {
    }

    public ListModelRequired(string name, string selectedValue):base(name,selectedValue)
    {
    }

    public ListModelRequired(string name, IEnumerable<SelectListItem> members)
        : base(name, members)
    {
    }

    public ListModelRequired(string name, IEnumerable<SelectListItem> members, string selectedValue)
        : base(name, members,selectedValue)
    {
    }
}

解决方案

I think that the default model binder gets this right, validating submodels that use the data annotations. What problem are you actually having it with it? For example, I have something similar to:

public class OnlineDonationModel
{
     [Required]
     public decimal? Amount { get; set; }

     public ContactModel Contact { get; set; }
}

public class ContactModel
{
     [Required]
     public string FirstName { get; set; }

     [Required]
     public string LastName { get; set; }

     [Required]
     public string Address { get; set; }

     ...
}

这篇关于ASP.NET MVC的复杂模型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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