0

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

0

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, fantastic - I thought it would be harder that that.
1

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>

Comments

0

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
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.