3

this class comes from db

public class Company
{
    public new Guid ID { get; set; }
    public new String Name { get; set; }
}

I want to use class CompanyModel as model in mvc but it doesn't works

public class CompanyModel : Company
{
    public new Guid ID { get { return base.ID; } set { base.ID = ID; } }

    [Required]
    [Display(Name = "Company Name")]
    public new String Name { get { return base.Name; } set { base.Name = Name; } }
}

Is there ways to do something like that?

4

1 回答 1

4

我建议您改用视图模型:

public class CompanyViewModel
{
    public new Guid ID { get; set; }

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

然后在您的控制器操作中,您可以在域 EF 模型和将传递给视图的视图模型之间进行映射。要简化此映射,您可以使用类似AutoMapper.

于 2013-07-01T07:58:22.597 回答