Asp.net Mvc设置包含的任何模型的属性

时间:2014-03-07 14:11:51

标签: asp.net-mvc design-patterns repository-pattern

我的几乎所有模型中都有is_valid属性。我想更新包含此属性的任何对象的is_valid属性?

我该怎么做?我应该学习存储库模式吗?

我的一个模型如下所示:

public class HrFileType
{
    public int Id { get; set; }

    [Display(Name = "Dosya Tipinin Adı")]
    [Required]
    public String Name { get; set; }

    [Display(Name = "İkaz Periyodu(Gün)")]        
    public Int64? NotificationPeriod { get; set; }

    [Display(Name = "Açıklama")]
    public string Note { get; set; }

    public bool is_valid { get; set; }

    public virtual ICollection<HrMapPersonFile> HrMapPersonFile { get; set; }

}

1 个答案:

答案 0 :(得分:2)

最直接的方法:

public class BaseModel 
{
    public bool is_valid { get; set; }
}

public class HrFileType : BaseModel 
{
    public int Id { get; set; }

    [Display(Name = "Dosya Tipinin Adı")]
    [Required]
    public String Name { get; set; }

    [Display(Name = "İkaz Periyodu(Gün)")]        
    public Int64? NotificationPeriod { get; set; }

    [Display(Name = "Açıklama")]
    public string Note { get; set; }

    public virtual ICollection<HrMapPersonFile> HrMapPersonFile { get; set; }

}

您的Update方法可以是:

void markValid(BaseModel model)
{
    model.is_valid = true;
}

<强>更新

对于您的文件上传案例: 创建另一个基类:

public class FileUploadBaseModel : BaseModel
{
    //properties you need for file upload
}

在你的模型中需要fileupload:

public class SomeModelName: FileUploadBaseModel 
{
    //properties specific for this model
}