Asp.net mvc MVC只验证2个(或多个)模型中的1个模型

Asp.net mvc MVC只验证2个(或多个)模型中的1个模型,asp.net-mvc,validation,data-annotations,model-validation,Asp.net Mvc,Validation,Data Annotations,Model Validation,现在,我的场景是,我只想在ContactID>0时验证联系人模型(次要),否则它将只验证组织模型(主要)。我不想为此编写自定义业务验证 请建议我是否可以在MVC结构中使用数据注释。我已经参考了一个模型中的另一个模型,如下所示 public class Organization { public Organization() { this.OrgStatus = "Active"; this.OrgContact = new Contact();

现在,我的场景是,我只想在ContactID>0时验证联系人模型(次要),否则它将只验证组织模型(主要)。我不想为此编写自定义业务验证

请建议我是否可以在MVC结构中使用数据注释。我已经参考了一个模型中的另一个模型,如下所示

public class Organization
{
    public Organization()
    {
        this.OrgStatus = "Active";
        this.OrgContact = new Contact();
    }
    public int OrganizationID { get; set; }

    [Required(ErrorMessage = "Organization Code is required")]
    [MaxLength(25)]
    public string OrgCode { get; set; }

    [Required(ErrorMessage = "Status is required")]
    [MaxLength(20)]
    public string OrgStatus { get; set; }

    public Contact OrgContact { get; set; }

}

public class Contact
{
    public Contact()
    {
        this.Title = "Mr.";
    }

    [Required]
    public int? ContactID { get; set; }

    [Required(ErrorMessage = "Please enter Title", AllowEmptyStrings = false)]
    [MaxLength(5)]
    public string Title { get; set; }

    [Required(ErrorMessage = "Please enter First Name", AllowEmptyStrings = false)]
    [MaxLength(25)]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Please enter Middle Name", AllowEmptyStrings = false)]
    [MaxLength(25)]
    public string MiddleName { get; set; }

    [Required(ErrorMessage = "Please enter Last Name", AllowEmptyStrings = false)]
    [MaxLength(25)]
    public string LastName { get; set; }
}

如果不使用自定义模型绑定器,就无法告诉MVC忽略嵌套模型上的验证属性(在您的案例中是
Contact
)。MVC将自动执行任何嵌套模型的验证规则


继承 如果问题是您希望使用具有验证属性的相同视图模型,但嵌套的
Contact
属性仅适用于接收某些页面上的发布值,那么您可以使用继承创建一个视图模型,其中包括
Contact
属性,而不包括该属性

// Example action methods
public ActionResult Page1(Organization requestModel)
{
    ...
}

public ActionResult Page2(OrganizationWithContact requestModel)
{
    ...
}
public class Organization
{
    public Organization()
    {
        this.OrgStatus = "Active";
        this.OrgContact = new Contact();
    }

    public int OrganizationID { get; set; }

    [Required(ErrorMessage = "Organization Code is required")]
    [MaxLength(25)]
    public string OrgCode { get; set; }

    [Required(ErrorMessage = "Status is required")]
    [MaxLength(20)]
    public string OrgStatus { get; set; }

    // No longer required here.
    // public Contact OrgContact { get; set; }
}

// New model, using inheritence to include `Contact` model
public class OrganizationWithContact : Organization
{
    public Contact OrgContact { get; set; }
}
在本例中,您可能有两个操作方法,一个接受具有
Contact
属性的视图模型,另一个不接受

// Example action methods
public ActionResult Page1(Organization requestModel)
{
    ...
}

public ActionResult Page2(OrganizationWithContact requestModel)
{
    ...
}
public class Organization
{
    public Organization()
    {
        this.OrgStatus = "Active";
        this.OrgContact = new Contact();
    }

    public int OrganizationID { get; set; }

    [Required(ErrorMessage = "Organization Code is required")]
    [MaxLength(25)]
    public string OrgCode { get; set; }

    [Required(ErrorMessage = "Status is required")]
    [MaxLength(20)]
    public string OrgStatus { get; set; }

    // No longer required here.
    // public Contact OrgContact { get; set; }
}

// New model, using inheritence to include `Contact` model
public class OrganizationWithContact : Organization
{
    public Contact OrgContact { get; set; }
}
您可以从您的
组织
模型中删除
联系人
属性,并利用继承性确保您有一个特定于发布
联系人
值的页面的模型和一个不发布该值的页面的模型

// Example action methods
public ActionResult Page1(Organization requestModel)
{
    ...
}

public ActionResult Page2(OrganizationWithContact requestModel)
{
    ...
}
public class Organization
{
    public Organization()
    {
        this.OrgStatus = "Active";
        this.OrgContact = new Contact();
    }

    public int OrganizationID { get; set; }

    [Required(ErrorMessage = "Organization Code is required")]
    [MaxLength(25)]
    public string OrgCode { get; set; }

    [Required(ErrorMessage = "Status is required")]
    [MaxLength(20)]
    public string OrgStatus { get; set; }

    // No longer required here.
    // public Contact OrgContact { get; set; }
}

// New model, using inheritence to include `Contact` model
public class OrganizationWithContact : Organization
{
    public Contact OrgContact { get; set; }
}

通过使用上述模型布局,您可以重复使用现有的验证属性。

如果不使用自定义模型绑定器,就无法告诉MVC忽略嵌套模型上的验证属性(在您的情况下,请与
联系)。MVC将自动执行任何嵌套模型的验证规则


继承 如果问题是您希望使用具有验证属性的相同视图模型,但嵌套的
Contact
属性仅适用于接收某些页面上的发布值,那么您可以使用继承创建一个视图模型,其中包括
Contact
属性,而不包括该属性

// Example action methods
public ActionResult Page1(Organization requestModel)
{
    ...
}

public ActionResult Page2(OrganizationWithContact requestModel)
{
    ...
}
public class Organization
{
    public Organization()
    {
        this.OrgStatus = "Active";
        this.OrgContact = new Contact();
    }

    public int OrganizationID { get; set; }

    [Required(ErrorMessage = "Organization Code is required")]
    [MaxLength(25)]
    public string OrgCode { get; set; }

    [Required(ErrorMessage = "Status is required")]
    [MaxLength(20)]
    public string OrgStatus { get; set; }

    // No longer required here.
    // public Contact OrgContact { get; set; }
}

// New model, using inheritence to include `Contact` model
public class OrganizationWithContact : Organization
{
    public Contact OrgContact { get; set; }
}
在本例中,您可能有两个操作方法,一个接受具有
Contact
属性的视图模型,另一个不接受

// Example action methods
public ActionResult Page1(Organization requestModel)
{
    ...
}

public ActionResult Page2(OrganizationWithContact requestModel)
{
    ...
}
public class Organization
{
    public Organization()
    {
        this.OrgStatus = "Active";
        this.OrgContact = new Contact();
    }

    public int OrganizationID { get; set; }

    [Required(ErrorMessage = "Organization Code is required")]
    [MaxLength(25)]
    public string OrgCode { get; set; }

    [Required(ErrorMessage = "Status is required")]
    [MaxLength(20)]
    public string OrgStatus { get; set; }

    // No longer required here.
    // public Contact OrgContact { get; set; }
}

// New model, using inheritence to include `Contact` model
public class OrganizationWithContact : Organization
{
    public Contact OrgContact { get; set; }
}
您可以从您的
组织
模型中删除
联系人
属性,并利用继承性确保您有一个特定于发布
联系人
值的页面的模型和一个不发布该值的页面的模型

// Example action methods
public ActionResult Page1(Organization requestModel)
{
    ...
}

public ActionResult Page2(OrganizationWithContact requestModel)
{
    ...
}
public class Organization
{
    public Organization()
    {
        this.OrgStatus = "Active";
        this.OrgContact = new Contact();
    }

    public int OrganizationID { get; set; }

    [Required(ErrorMessage = "Organization Code is required")]
    [MaxLength(25)]
    public string OrgCode { get; set; }

    [Required(ErrorMessage = "Status is required")]
    [MaxLength(20)]
    public string OrgStatus { get; set; }

    // No longer required here.
    // public Contact OrgContact { get; set; }
}

// New model, using inheritence to include `Contact` model
public class OrganizationWithContact : Organization
{
    public Contact OrgContact { get; set; }
}

通过使用上述模型布局,您可以重复使用现有的验证属性。

当您说不想编写自定义业务验证时,你是什么意思?意思是说,通过像通常使用if-else那样编写代码,我就可以编写它了…但这些东西只解决了我的一个模型的需要…我正在寻找一个通用的解决方案。通过编写一个helper方法来编写验证代码。其中一个参数应该是viewmodel,另一个参数应该是modelstate,以便向其中添加错误。通过这样做,您可以使您的验证代码可重用。请您用任何示例解释一下……我添加了一个使用继承性的答案,允许您使用您已经用其装饰模型的验证属性。我得到的印象是,你关心的是重复你自己,所以这应该可以解决这个问题。最终的结果是,您有两个模型要绑定—一个具有Contact属性,另一个不具有Contact属性,但通过使用相同的验证属性。当您说不想编写自定义业务验证时,你是什么意思?意思是说,通过像通常使用if-else那样编写代码,我就可以编写它了…但这些东西只解决了我的一个模型的需要…我正在寻找一个通用的解决方案。通过编写一个helper方法来编写验证代码。其中一个参数应该是viewmodel,另一个参数应该是modelstate,以便向其中添加错误。通过这样做,您可以使您的验证代码可重用。请您用任何示例解释一下……我添加了一个使用继承性的答案,允许您使用您已经用其装饰模型的验证属性。我得到的印象是,你关心的是重复你自己,所以这应该可以解决这个问题。最终的结果是,您有两个模型要绑定-一个具有Contact属性,另一个不具有Contact属性,但通过使用相同的验证属性。遗憾的是,如果不编写验证模型的业务逻辑,就无法做到这一点。不过,此验证肯定可以重复使用!如果你愿意,我可以给你举个例子,说明你将如何做到这一点。遗憾的是,如果不编写验证模型的业务逻辑,就无法做到这一点。不过,此验证肯定可以重复使用!如果你愿意,我可以给你举个例子,说明你将如何做到这一点?