我在下面创建了一个 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 它告诉我我尚未完全实现该界面是否需要在视图模型或界面或两者中放置任何东西来告诉他们我使用 AutoMapper 在这两个变量之间创建映射?
以下是我正在使用的 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; }
}