public class ClientViewModel
{
[Required(ErrorMessage = "The Client Code field is required.")]
public string ClientCode { get; set; }
[Required(ErrorMessage = "The Company Legal Name field is required.")]
public string CompanyLegalName { get; set; }
public string Notes { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public Nullable<DateTime> ScheduledDate { get; set; }
public Nullable<decimal> AmountDiscount { get; set; }
}
public class Client
{
public string ClientCode { get; set; }
public string CompanyLegalName { get; set; }
public string Notes { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public Nullable<DateTime> ScheduledDate { get; set; }
public Nullable<decimal> AmountDiscount { get; set; }
}
Edit:
Exception Details: AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types: Client -> ClientViewModel myapp.Models.Client -> myapp.Models.ClientViewModel
Destination path: ClientViewModel
Source value: myapp.Models.Client
My Client
& ClientViewModel
has exact same number of props and below is my code that I'm using and its throwing error without getting much information, what I'm missing here?
Client client = context.Clients.Where(x => x.CustomerID == id).FirstOrDefault();
ClientViewModel clientViewModel = Mapper.Map<Client, ClientViewModel>(client);
An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code
You just forgot to create your map. Add this to your code (before the call of the Mapper
class) :
Mapper.CreateMap<Client, ClientViewModel>();
ClientViewModel cvm = Mapper.Map<Client, ClientViewModel>(client);
Working demo on dotnetfiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With