1

大多数教程根本没有真正涵盖这一点。他们只是说将您的实体链接到控制器,您就完成了。

在我的业务模型中,我有客户,我有客户联系人 1 个客户到 >1 个客户联系人。我如何为这些创建一个视图模型,以允许它们从同一个视图中编辑/创建/任何内容?

public class Customer
{
    public Customer()
    {
        this.CustomerContacts = new List<CustomerContact>();
        this.Systems = new List<System>();
        this.CreatedByCustomerTickets = new List<Ticket>();
        this.CustomerTickets = new List<Ticket>();
    }

    public long CustomerID { get; set; }
    public Nullable<bool> BusinessCustomer { get; set; }
    public string CustomerName { get; set; }
    public string CustomerNotes { get; set; }
    public virtual ICollection<CustomerContact> CustomerContacts { get; set; }
    public virtual ICollection<System> Systems { get; set; }
    public virtual ICollection<Ticket> CreatedByCustomerTickets { get; set; }
    public virtual ICollection<Ticket> CustomerTickets { get; set; }
}

public class CustomerContact
{
    public long CustomerContactID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Phone { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public Nullable<int> Zip { get; set; }
    public Nullable<long> CustomerID { get; set; }
    public string Email { get; set; }
    public bool PromotionalEmails { get; set; }
    public virtual Customer Customer { get; set; }
}
4

1 回答 1

1

好吧,我会从这个开始

public class CustomerViewModel
{
  public Customer Customer {get; set;}
  public CustomerContact CustomerContact {get; set;}
}

并从那里工作。

如果您不需要域对象的所有属性,您可以考虑更多类似的东西:

public class CustomerViewModel
    {
       public long CustomerID { get; set; }
       public ICollection<CustomerContact> CustomerContacts { get; set; }
    }

以满足特定项目需求的方式构建视图模型真的取决于您。

于 2013-01-31T19:06:44.560 回答