0

客户.cs

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

城市.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 转换为 Customer 的函数,如下所示:

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, CustomerName, ...) ,所以这是不好的方法。

当我使用 DbContextGenerator 并将类添加到 dataAnnotations 然后当我更新模型时, dataAnnotations 被删除。(因为 DbContext 类也更新了)

如何将 ViewModel 与 DataAnnotations 一起使用。以及对 Db 的有效插入操作?文章、教程、示例或建议?

我希望我能解释一下。

非常感谢...

4

1 回答 1

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);
}
于 2012-10-15T08:38:58.983 回答