Reputation: 3199
If I have an MVC3 ASP.net page which contains a the model "iEnumerable<mymodel.CarsForHire>
" - which links directly into an entity framework model- and I also now want to include the default user management model that MVC3 generates, how can I use both models at once?
I assume I will have to pass a collection in, or create a model called "blah
" which has a field "carsforHire" which marries up to iEnumerable<mymodel.CarsForHire>
, but not sure where to start.
Any ideas would be greatly appreciated
Upvotes: 0
Views: 364
Reputation: 8748
What I usually do is create a ViewModel class. For example:
public class CarsViewModel
{
public IEnumerable<mymodel.CarsForHire> CarsForHire { get; set; }
public UserModel User { get; set; }
}
Then create the view with CarsViewModel
instead of IEnumerable<mymodel.CarsForHire>
Upvotes: 1
Reputation: 13673
You already mentioned the solution; create a view model class that holds all the required data. For example:
public class SomeViewModel
{
public User CurrentUser { get; set; }
public IEnumerable<mymodel.CarsForHire> Cars { get; set; }
}
Construct that model in the controller and pass it to your view. And don't forget to update the type declaration at the top of your view.
Upvotes: 3
Reputation: 5186
You can use Navigational properties of EF if you want to.
i.e
public class carForHire()
{
int property1{get; set;}
.
.
.
//Navigationla property
public virtual blah blah { get; set; }
}
at your page/view you can acces it like
@foreach(var item in Mode)
{
item.blah.blahProperty
}
Upvotes: 0