Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
i want use multiple model in one view and add records in multiple table from view my db like below image: (i work with vs2012 mvc4 and EF)


http://i61.tinypic.com/n17580.png[^]
i create four model for each table and class "PreOrder" for repository of all

C#
public class Orders
{
    public long OrdersId { get; set; }
    public string CustomerId { get; set; }
    public long OrderListId { get; set; }
    public int? CountProduct { get; set; }
    public string CountPrice { get; set; }
    public string VisitorsName { get; set; }
    public DateTime? OrderDate { get; set; }
}

 public class Product
{
    public string ProductID { get; set; }
    public string NameProduct { get; set; }
    public string Price { get; set; }
}
  public class Customers
{
    public string CustomerID { get; set; }
    [DisplayFormat(NullDisplayText = "-")]
    public string Name { get; set; }
    [DisplayFormat(NullDisplayText = "-")]
    ...
}
public class OrderList
{
    public long OrderListID { get; set; }
    public Nullable<long> OrdersId { get; set; }
    public string ProductId { get; set; }
    public Nullable<int> Count { get; set; }
    public Nullable<decimal> DisCount { get; set; }
}
public class PreOrder
{
    public Customers _Customer { set; get; }
    public Product _Product { set; get; }
    public Orders _Order { set; get; }
    public OrderList _OrderList { set; get; }
  }


i want use name,family,customerid from tblcustomers and productId,NameProduct,Price from tblProducts and all fields of tblOrders and tblOrderList how can i create one view to fill tables Orders and OrderList??
Posted
Updated 13-Feb-14 22:39pm
v3

1 solution

For that you have to create ViewModel.Then fill that ViewModel from your domain models data and bind that into your view (i.e. ViewModel to View).

What is a ViewModel ?

# Allow you to shape multiple entities from one or more data models or sources into a single object

# Optimized for consumption and rendering by the view

I have written an article about this.Please check that for more info.

How to Use ViewModel with ASP.NET MVC ?

How to Use ValueInjecter with Asp.net MVC ViewModel ?
 
Share this answer
 
v2
Comments
JoCodes 14-Feb-14 5:04am    
5 Nice one Sampath. Was about to post it . :)
Sampath Lokuge 14-Feb-14 5:06am    
Thanks JoCodes :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900