suman
suman

Reputation: 728

A single view with two models in asp.net MVC 4

I have a class called movie.cs in the modal which is as follows.

namespace CodFirst.Models
{
    public class Movie
    {
        public int MovieId { get; set; }
        public string name { get; set; }

        public string genre { get; set; }
    }

    public class Student
    {

        public int StudentId { get; set; }
        public string fname { get; set; }
        public string lname { get; set; }
    }

}

And i have a context class in the same modal called moviecontext as:

namespace CodFirst.Models
{
    public class MovieContext:DbContext
    {
        public DbSet<Movie> Movies { get; set; }
        public DbSet<Student> Students { get; set; }
    }
}

Now i want to access the data of both Movies and student in the same view.But i am not able to access data of any one either.

IN the controller i have

 MovieContext db = new MovieContext();

    public ActionResult Index()
    {
        //db.Movies.ToList();
        var myvar = db.Movies;

        return View(myvar);
    }

And finally the view is as follows:

 @model CodFirst.Models.MovieContext

@{
    ViewBag.Title = "Index";
}

@foreach(var item in Model.Movies)
{
  @Html.DisplayName("movie: ") @item.name;  @Html.DisplayName(" type: ") @item.genre;  <br />

}

Upvotes: 0

Views: 187

Answers (4)

devqon
devqon

Reputation: 13997

A good practice with this kind of stuff is using so called 'ViewModels'. Define a new model that holds your data that you want to access in your view:

MyViewModel:

public class MyViewModel {
    public IEnumerable<Movie> Movies { get; set; }
    public IEnumerable<Student> Students { get; set; }
}

Controller:

public ActionResult Index()
{
    var model = new MyViewModel {
        Movies = db.Movies.ToList(),
        Students = db.Students.ToList()
    }
    return View(model);
}

Then the view:

@model CodFirst.Models.MyViewModel

// your view stuff

Upvotes: 2

Neel
Neel

Reputation: 11741

its happening because you are expecting MovieContext in view and you are passing Movie model so try making viewmodel as shown in below code :-

 public class ViewModel
{
    public IEnumerable<Movie> Movies { get; set; }
    public IEnumerable<Student> Students { get; set; }
}

and view :-

 @model CodFirst.Models.ViewModel

@{
    ViewBag.Title = "Index";
}

@foreach(var item in Model.Movies)
{
  @Html.DisplayName("movie: ") @item.name;  @Html.DisplayName(" type: ") @item.genre;  <br />

}

Upvotes: 0

Vivekh
Vivekh

Reputation: 4259

You can go with View Model but if you really wanna use Tuple here you go

public class ToupleController : Controller
{
    public ActionResult Index()
    {
        var first = new FirstModel();
        var second = new SecondModel();

        return View(Tuple.Create(first,second));
    }
}

and in your view

    @model Tuple<FirstModel, SecondModel>
    <div> 
        @Html.TextBoxFor(x => x.Item1.Prop1)
        @Html.TextBoxFor(x => x.Item1.Prop2)
    </div>
   // to access your second model
    <div> 
        @Html.TextBoxFor(x => x.Item2.Prop1)
        @Html.TextBoxFor(x => x.Item2.Prop2)
    </div>

Upvotes: 1

Marco
Marco

Reputation: 23945

What you need is a ViewModel. A Viewmodel class consists of those classes you want to display in your case: Movies and Students. It can be used to only wrap those information you need or to be a wrapper for more complex data.

public class MovieStudentVM
{
    public IEnumerable<Movie> Movies { get; set; }
    public IEnumerable<Student> Students { get; set; }
}

Your Index method should now return your Viewmodel:

public ActionResult Index()
{
    var vm = new MovieStudentVM();
    vm.Movies = db.Movies;
    vm.Students = db.Students;

    return View(vm);
}

After this just build up your View:

@model CodFirst.Models.MovieStudentVM

@foreach(var item in Model.Movies)
{
  @Html.DisplayName("movie: ") @item.name;  @Html.DisplayName(" type: ") @item.genre;  <br />
}

@foreach(var item in Model.Students)
{
  @* ... *@
}

Further information:

Upvotes: 3

Related Questions