Nic*_*ahn 6 c# asp.net-mvc automapper
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; }
}
Run Code Online (Sandbox Code Playgroud)
编辑:
异常详细信息:AutoMapper.AutoMapperMappingException:缺少类型映射配置或不支持的映射.
映射类型:客户端 - > ClientViewModel myapp.Models.Client - > myapp.Models.ClientViewModel
目标路径:ClientViewModel
来源值:myapp.Models.Client
我的Client&ClientViewModel具有完全相同数量的道具,下面是我正在使用的代码和抛出错误而没有获得太多信息,我在这里缺少什么?
Client client = context.Clients.Where(x => x.CustomerID == id).FirstOrDefault();
ClientViewModel clientViewModel = Mapper.Map<Client, ClientViewModel>(client);
Run Code Online (Sandbox Code Playgroud)
AutoMapper.dll中出现"AutoMapper.AutoMapperMappingException"类型的异常,但未在用户代码中处理
你忘了创建你的地图.将其添加到您的代码中(在调用Mapper类之前):
Mapper.CreateMap<Client, ClientViewModel>();
ClientViewModel cvm = Mapper.Map<Client, ClientViewModel>(client);
Run Code Online (Sandbox Code Playgroud)
在调用 Map 之前。您需要调用CreateMap:
Mapper.CreateMap<Client, ClientViewModel>();
Run Code Online (Sandbox Code Playgroud)
通常,您会在应用程序初始化代码/类中调用它,例如在 global.asax.cs 中。
| 归档时间: |
|
| 查看次数: |
23446 次 |
| 最近记录: |