0

I have created a AutoMapper mapping configuration class code below:

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

I am mapping the field Rank in the MilitaryBrochureViewModel to the variable JobTitle in the IBrochureModel interface when I navigate to the MilitaryBrochureViewModel it tells me I have not fully implemented the interface do I need to place anything in either the viewmodel or interface or both to tell them I have used AutoMapper to create a mapping between these two variables?

below are the ViewModel and Interface that I am using:

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

Interface

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; }
}
CryoFusion87
  • 796
  • 1
  • 8
  • 28

3 Answers3

0

Your Interface requires member string JobTitle { get; set; }. You do not have any such member on MilitaryBrochureViewModel. The C# compiler doesn't care about AutoMapper's translation of one prop to another, it is just erroring out because you have a class that is implementing an interface but missing one of the interface members.

Graham
  • 3,217
  • 1
  • 27
  • 29
0

add below line in to your viewmodel.

public string JobTitle { get; set; }

This way your viewmodel will have Rank and Jobtitle.

You can control on view what field JobTitle or Ran to display

swapneel
  • 3,061
  • 1
  • 25
  • 32
0

have changed to the way I did it in the previous version of this application changed rank to jobtitle the only difference being the display name since rank and jobtitle are both sent to the database as jobtitle anyway.

    [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; }
CryoFusion87
  • 796
  • 1
  • 8
  • 28