且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从模型验证中排除字段

更新时间:2023-11-28 22:20:22

您可以使用

ModelState.Remove("Email");

删除模型状态下与隐藏字段相关的条目.

to remove entries in model state, that are related to hidden fields.

***的解决方案是将视图模型分为两个:

The best solution is to divide view model into two:

public class PersonViewModel
{
    [Required]
    public String FirstName { get; set; }

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

public class PersonWithEmailViewModel : PersonViewModel
{
    [Required]
    public String Email { get; set; }
}