Click here to Skip to main content
15,905,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know that it can be done by keeping one List property into another, Like Below:

C#
Class Customer
{
 public string Name {get; set;}
 Public List<Address> Address {get; set;}

}

Class Address
{
 public string StreetNo {get; set;}
 public string HouseNo {get; set;}
}


And then, @model Customer in View and @Model.Name and @foreach(var item in Address){item.StreetNo} to render values or keeping a ViewModel or ModelBinder.

But my specific question is if I keep like below in a view,

@model Customer
@model Address
-- in Customer => Index.cshtml for the below model classes.

C#
Class Customer
{
 public string Name {get; set;}

}

Class Address
{
 public string StreetNo {get; set;}
 public string HouseNo {get; set;}
}


Will not work. Why ?

In this case, the same view should render for two different model objects and in my CustomerController => Action method() I will either return Customer or an Address object based on condition(if/else). I do not want to use partial view.

The specific question is Why we can not have two models in a View ? How MVC internally stops to do this/not accept to do this ?

Any guidence would be appriciable.
Posted
Updated 29-Apr-15 1:16am
v2

1 solution

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