I know that it can be done by keeping one List property into another, Like Below:
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.
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.