How can we use two(multiple) models in single view

Let us first try to understand what the interviewer is asking. When we bind a model with a view we use the model dropdown as shown in the below figure. In the below figure we can only select one model.

But what if we want to bind “Customer” as well as “Order” class to the view.

For that we need to create a view model which aggregates both the classes as shown in the below code. And then bind that view model with the view.

public class CustOrderVM
{
public   Customer cust = new  Customer();
public  Order Ord = new Order();
}

In the view we can refer both the model using the view model as shown in the below code.

<%= model.cust.Name %>
<%= model.Ord.Number %>

results matching ""

    No results matching ""