尝试在viewmodel和我的模型之间进行映射。在这种情况下,我将从Web服务(创建强类型视图)中获取数据,然后将其加载到表单中。然后,我从Web服务验证客户端数据,提交表单。它将根据我的数据库中是否有记录进行插入或更新。
我一直想使用AutoMapper一段时间,所以这是我第一次使用它。我希望将SearchResult的属性映射到我的客户端模型。
namespace Portal.ViewModels
{
public class ClientSearch
{
public SearchForm searchForm { get; set; }
public SearchResult searchResult { get; set; }
}
public class SearchForm
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string DOB { get; set; }
public string AccNumber { get; set; }
}
public class SearchResult
{
public int ClientID { get; set; }
public string AccNumber { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Phone { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Gender { get; set; }
public string DOB { get; set; }
public int Age { get; set; }
}
}
namespace Portal.Models
{
public class Client
{
public int ClientID { get; set; }
public string AccNumber { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Phone { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Gender { get; set; }
[Display(Name = "Date of Birth")]
public DateTime DOB { get; set; }
public int Age { get; set; }
}
}
在我的控制器中,我正在尝试使用AutoMapper将clientSearch中的数据映射到基于HttpPost的客户端模型。
但是,我收到错误:在尝试创建Map时,只能使用赋值,调用,递增,递减,等待和新对象表达式作为语句。这是我尝试使用AutoMapper。
[HttpPost]
public ActionResult Process(ClientSearch clientSearch)
{
// Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
Mapper.CreateMap<ClientSearch, Client>;
Client client = Mapper.Map<ClientSearch, Client>(clientSearch);
ClientRepository.InsertOrUpdate(client);
ClientRepository.Save();
}
答案 0 :(得分:1)
你似乎错过了'()'之后:
Mapper.CreateMap<ClientSearch, Client>;
即。它应该是:
Mapper.CreateMap<ClientSearch, Client>();