2012-10-15 115 views
0

Customers.csASP.NET MVC什麼是使用ViewModels的最佳方式?

public partial class Customers 
{ 
    public int sno { get; set; } 
    public string CustomerName { get; set; } 
    public string CustomerNo { get; set; } 
    ... 
    // 20 more attribute too... 
} 

Cities.cs

public partial class Cities 
{ 
    public int sno { get; set; } 
    public string CityName { get; set; } 
    public string CityPlate { get; set; } 
    public string CityPhoneCode { get; set; } 
} 

AddCustomerViewModel.cs

public class AddCustomerViewModel 
{ 

    [Required(ErrorMessage = "Şehir seçiniz.")] 
    [Display(Name = "Şehir")] 
    public Nullable<int> CityId { get; set; } 

    // same with Customers.cs 
    public int sno { get; set; } 

    [Required(ErrorMessage = "Müşteri adını giriniz!")] 
    [Display(Name = "Müşteri Adı")] 
    public string CustomerName { get; set; } 

    [Required(ErrorMessage = "Müşteri numarası giriniz!")] 
    [Display(Name = "Müşteri Numarası")] 
    public string CustomerNo { get; set; } 
    ... 
    // 20 more attribute too... 
} 

控制器

[Authorize(Roles = "Administrator")] 
public ActionResult AddCustomer() 
{ 
    AddCustomerViewModel addCustomerViewModel = new AddCustomerViewModel(); 
    addCustomerViewModel.Cities = entity.Cities; 

    return View(addCustomerViewModel); 
} 

[HttpPost] 
[Authorize(Roles = "Administrator")] 
public ActionResult AddCustomer(AddCustomerViewModel addCustomerViewModel) 
{ 
    entity.Customers.Add(GetCustomerFromViewModel(addCustomerViewModel)); 
    entity.SaveChanges(); 

    return View(addCustomerViewModel); 
} 

我米使用被稱爲GetCustomerFromViewModel到addCustomerViewModel轉換爲客戶像下面的函數:)

GetCustomerFromViewModel(

private Customers GetCustomerFromViewModel(AddCustomerViewModel addCustomerViewModel) 
{ 
    Customers customer = new Customers(); 

    customer.CityId = addCustomerViewModel.CityId; 
    customer.CreatorUserId = (Guid)System.Web.Security.Membership.GetUser().ProviderUserKey; 
    customer.CustomerName = addCustomerViewModel.CustomerName; 
    customer.CustomerNo = addCustomerViewModel.CustomerNo; 
    customer.Description = addCustomerViewModel.Description; 
    ... 
    // 20 more attribute too... 

    return customer; 
} 

但客戶類有太多的變量(customerNo,客戶名稱,...),所以這是不好的方法。

當我使用DbContextGenerator並將類添加到dataAnnotations,然後當我更新模型時,dataAnnotations被刪除。 (因爲DbContext類也被更新)

如何在DataAnnotations中使用ViewModels。對Db有效插入操作?文章,教程,示例或建議?

我希望我能解釋一下。

非常感謝......

回答

3

你可以看看AutoMapper,這將簡化您的域模型和視圖模型之間的映射邏輯,這樣你就不需要每個屬性手動映射。除此之外,您的代碼沒有任何問題。您已經在使用視圖模型並具有映射圖層。所以你GetCustomerFromViewModel功能可能會成爲:

private Customers GetCustomerFromViewModel(AddCustomerViewModel addCustomerViewModel) 
{ 
    return Mapper.Map<AddCustomerViewModel, Customers>(addCustomerViewModel); 
} 

或完全擺脫它,並直接使用AutoMapper呼叫控制器操作,因爲此功能不再帶來多少價值:

[HttpPost] 
[Authorize(Roles = "Administrator")] 
public ActionResult AddCustomer(AddCustomerViewModel addCustomerViewModel) 
{ 
    var customer = Mapper.Map<AddCustomerViewModel, Customers>(addCustomerViewModel); 
    entity.Customers.Add(customer); 
    entity.SaveChanges(); 

    return View(addCustomerViewModel); 
} 
+0

我的問題是使用GetCustomerFromViewModel函數將ViewModel轉換爲DomainModel –

+0

是的,AutoMapper將簡化這一操作,因此不需要手動設置每個屬性。 –

+0

錯誤:缺少類型映射配置或不支持的映射。 映射類型: AddCustomerViewModel - >客戶 OsosPlus.Models.ViewModels.AddCustomerViewModel - > OsosPlus.Models.EntityModels.Customers 目標路徑: 客戶 來源值: OsosPlus.Models.ViewModels.AddCustomerViewModel –

相關問題