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

本文关键字:模型 映射 接口 视图 AutoMapper 使用 | 更新日期: 2023-09-27 18:06:06

我已经在下面创建了一个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));
    }
}

我正在映射字段秩在军事brochureviewmodel到变量JobTitle在IBrochureModel接口当我导航到军事brochureviewmodel它告诉我我还没有完全实现的接口我需要在视图模型或接口或两者中放置任何东西告诉他们我已经使用AutoMapper创建这两个变量之间的映射?

下面是我使用的ViewModel和Interface:

ViewModel

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; }
}

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

您的接口需要成员string JobTitle { get; set; }。您在MilitaryBrochureViewModel上没有这样的成员。c#编译器并不关心AutoMapper将一个prop转换为另一个prop,它只是出错了,因为你有一个类正在实现一个接口,但缺少一个接口成员。

将下面的line in添加到视图模型中。

public string JobTitle { get; set; }
这样你的视图模型就会有RankJobtitle。

你可以控制查看哪个字段JobTitle或Ran显示

已更改为我在此应用程序的前一个版本中将rank更改为jobtitle的方式,唯一的区别是显示名称,因为rank和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; }