使用AutoMapper将视图模型映射到模型接口

时间:2013-03-26 10:45:57

标签: c# asp.net-mvc-4 automapper-2

我在下面创建了一个AutoMapper映射配置类代码:

public static class MilitaryBrochureViewModelMapper
{
    public static void Configure()
    {
        ConfigureMilitaryBrochureMapping();
    }

    private static void ConfigureMilitaryBrochureMapping()
    {
        Mapper.CreateMap<IBrochureModel, MilitaryBrochureViewModel>().ForMember(dest => dest.Rank, opt => opt.MapFrom(src => src.JobTitle));
    }
}

当我导航到MilitaryBrochureViewModel时,我将MilitaryBrochureViewModel中的字段Rank映射到IBrochureModel接口中的变量JobTitle它告诉我我没有完全实现接口我是否需要在viewmodel或interface中放置任何内容告诉他们我用AutoMapper创建了这两个变量之间的映射?

下面是我正在使用的ViewModel和Interface:

视图模型

public class MilitaryBrochureViewModel : IBrochureModel
{
    [Key]
    public int Id { get; set; }

    public virtual int TestMode { get; set; }

    [DataType(DataType.Text)]
    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "required", ErrorMessageResourceType = typeof(Mui))]
    [Display(Name = "Salutation", Description = "Salutation_Description", GroupName = "Personal", ResourceType = typeof(Mui))]
    public string Salutation { get; set; }

    [DataType(DataType.Text)]
    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "required", ErrorMessageResourceType = typeof(Mui))]
    [Display(Name = "First_Name", Description = "First_Name_Description", GroupName = "Personal", ResourceType = typeof(Mui))]
    [RegularExpression(@"^([\u00c0-\u024f]|[A-z]|[']|[ ]|[\-]){2,20}$", ErrorMessageResourceName = "FirstNameRegExError", ErrorMessageResourceType = typeof(Mui))]
    public string FirstName { get; set; }

    [DataType(DataType.Text)]
    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "required", ErrorMessageResourceType = typeof(Mui))]
    [Display(Name = "Last_Name", Description = "Last_Name_Description", GroupName = "Personal", ResourceType = typeof(Mui))]
    [RegularExpression(@"^([\u00c0-\u024f]|[A-z]|[']|[ ]|[\-]){2,20}$", ErrorMessageResourceName = "LastNameRegExError", ErrorMessageResourceType = typeof(Mui))]
    public string LastName { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "required", ErrorMessageResourceType = typeof(Mui))]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email", Description = "Email_Description", GroupName = "Personal", ResourceType = typeof(Mui))]
    [RegularExpression(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", ErrorMessageResourceName = "EmailInputValidatorError", ErrorMessageResourceType = typeof(Mui))]
    public virtual string Email { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "required", ErrorMessageResourceType = typeof(Mui))]
    [DataType(DataType.Text)]
    [Display(Name = "Military_Rank", Description = "Military_Rank_Description", GroupName = "Personal", ResourceType = typeof(Mui))]
    [StrMinLen(5)]
    [StrMaxLen(50)]
    public string Rank { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "required", ErrorMessageResourceType = typeof(Mui))]
    [DataType(DataType.Text)]
    [Display(Name = "Company", Description = "Company_Description", GroupName = "Personal", ResourceType = typeof(Mui))]
    public string Company { get; set; }

    [Required]
    [Display(Name = "Do_Not_Pass_On", Description = "Do_Not_Pass_On_Description", GroupName = "Company", ResourceType = typeof(Mui))]
    public bool DoNotPassOn { get; set; }

    [DataType(DataType.Text)]
    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "required", ErrorMessageResourceType = typeof(Mui))]
    [Display(Name = "Lead_Source", GroupName = "Terms", Description = "Lead_Source_Description", ResourceType = typeof(Mui))]
    public string LeadSource { get; set; }
}

接口

public interface IBrochureModel
{
    int TestMode { get; set; }

    string Salutation { get; set; }
    string FirstName { get; set; }
    string LastName { get; set; }
    string Email { get; set; }
    string Company { get; set; }
    string JobTitle { get; set; }
    string LeadSource { get; set; }
    bool DoNotPassOn { get; set; }
}

3 个答案:

答案 0 :(得分:0)

您的界面需要会员string JobTitle { get; set; }。您在MilitaryBrochureViewModel上没有任何此类成员。 C#编译器并不关心AutoMapper将一个prop转换为另一个prop,它只是错误的,因为你有一个实现接口但缺少一个接口成员的类。

答案 1 :(得分:0)

将以下行添加到您的viewmodel。

public string JobTitle { get; set; }

这样,您的视图模型将具有排名作业标题。

您可以在视图上控制JobTitle或Ran显示哪个字段

答案 2 :(得分:0)

已更改为我在此应用程序的先前版本中执行此操作的方式已更改排名为jobtitle唯一的区别是显示名称,因为排名和作业标记都作为jobtitle发送到数据库。

    [Required(AllowEmptyStrings = false, ErrorMessageResourceName = "required", ErrorMessageResourceType = typeof(Mui))]
    [DataType(DataType.Text)]
    [Display(Name = "Military_Rank", Description = "Military_Rank_Description", GroupName = "Personal", ResourceType = typeof(Mui))]
    [StrMinLen(5)]
    [StrMaxLen(50)]
    public string JobTitle { get; set; }