jpo
jpo

Reputation: 4059

MVC3 View with multiple models

I have a model for a view. This view contains a an @Html.DropDownList whose values are in a model. How can I have my view take both model for different parts of the program?

For example, I have

@model IEnumerable<projectname.mymodel>
@{
   ViewBag.Title = "my title"
 }
 <!--This should use a different model-->
 <p>@Html.DropDownList(m =>m.name), new set(Model.list, "value", "key"))

If I have model1 and model2(connects to database)-all stored in the Model folder; and a big model class as something like

public class MyViewModel {
   public model1 var1 { get; set; }
   public IEnumerable<projectname.Model.model2> var2 { get; set; }
}

And i call this model in my view:

@model ProjectName.Models.Models

How do I reference an item in model1 or model2?

Upvotes: 1

Views: 448

Answers (1)

Jan
Jan

Reputation: 16048

Just pass a new class as model to your view which contains all the data you need:

public class MyViewModel {
    public SelectList list { get; set; }
    public IEnumerable<projectname.mymodel> Items { get; set; }
}

Another approach would be to pass the list in the Viewbag:

Viewbag.list = new SelectList() ...

Upvotes: 3

Related Questions