0
試圖在viewmodel和我的模型之間進行映射。在這種情況下,我從Web服務中獲取數據(創建強類型視圖),然後將其加載到表單中。然後,我從Web服務驗證客戶端數據,提交表單。它會根據數據庫中是否有記錄來執行插入或更新操作。具有多個屬性的Model和ViewModel之間的AutoMapper
我一直有意使用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我的客戶端模式。
但是,我收到錯誤:嘗試創建映射時,只能使用賦值,調用,增量,減量,等待和新對象表達式作爲語句。這是我使用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();
}
萬一你沒有注意到,你的編譯器告訴你對這個。基本上,只有賦值,調用,遞增,遞減,等待和新對象表達式可以用作語句,它告訴你寫的內容不是語句。這其實很精確;) – Kjellski