如何删除父类的必需字段验证 [英] How to remove required field validation of a parent class

查看:80
本文介绍了如何删除父类的必需字段验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





我有两个类,其中包含ListName的必填字段。一个是父类。如何避免或逃避Parent类的验证。我试过了

ModelState.Remove(ContactModel_ListName);但它没有用。



以下是我的模特课:



  public   class  ContactModel:BaseModel 
{
public int UserId { get ; set ; }
public int ListId { get ; set ; }


[必需]
[显示(名称= 列表名称)]
public string ListName {获得; set ; }
}


public class ContactUserModel
{
public ContactModel ContactModel { get ; set ; }

}

解决方案

我认为你不能这样做。或者,您可以在派生类中为此属性设置一些默认值

您可以通过不指定属性创建另一个属性为非必需的模型类,并在任何地方使用该模型类不是必填字段。

谢谢


Hi All,


I have a two classes which is having Required field for ListName. One is a Parent class. How can I avoid or escape the validation for Parent class. I have tried with
ModelState.Remove("ContactModel_ListName"); But its not working.

Below is my model class:

public class ContactModel : BaseModel
   {
       public int UserId { get; set; }
       public int ListId { get; set; }

      
       [Required]
       [Display(Name = "List Name")]
       public string ListName { get; set; }
}


public class ContactUserModel
    {
        public ContactModel ContactModel { get; set; }
               
    }

解决方案

I don't think you can do this. Alternatively you can set some default value for this property in your derived classes


You can create another model class with the properties as not Required by not specifying the attribute and use that model class wherever that will not be a mandatory field.
Thanks


这篇关于如何删除父类的必需字段验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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