Vrashabh Irde
Vrashabh Irde

Reputation: 14367

Join two models to get data into a view

Complete MVC Noob warning.(2 hours learn time)

I've looked at a lot of MVC3 examples online but I havent found a simple example to do what I am trying to do.

What I want to do is two join two models and get some data into a view. The most obvious

  public partial class Model1
    {
        public int ID { get; set; }
        public int StudentID { get; set; }
        public int CoachID { get; set; }
        public String StudentName {get;set;}

}



public partial class Model2
    {
        public int CoachID { get; set; }
        public String CoachName { get; set; }
}

Basically in my view I have to just join Model 1 and model 2 on CoachID and print StudentName and CoachName in a grid.

  1. How do I do this? How do I create the view model for this ?

  2. How do I iterate through the view and print the joined data?

  3. Can I instead just create a view in the database and directly attach a model and a view to it ?

Sounds simple, but Ive spent the last three hours online completely baffled

Upvotes: 4

Views: 10956

Answers (3)

Adam Tuliper
Adam Tuliper

Reputation: 30152

  1. Create a StudentCoachViewModel with exactly the properties you need in it to display, nothing more, nothing less.

  2. Populate a list of this viewmodel in your controller and send it to your view. Code sample to follow shortly.

  3. Enumerate that list in your view


public class StudentCoachViewModel
{
   public string CoachName { get; set; }
   public string StudentName { get;set; }
}

In your controller something along the following lines (just typing this out, haven't checked in compiler)

public ActionResult Index()
{

  //code to populate your model1 and model2 already assumed

  var viewModels = (from m in model1List
                 join r in model2List on m.CoachId equals r.CoachId 
                 select new StudentCoachViewModel(){ StudentName=m.StudentName, 
                          CoachName = r.CoachName }).ToList();

  return View(viewModels);
}

In your view, something along the lines of (clearly you want to format and use proper layout, table, etc which can be auto generated by visual studio)

@model IEnumerable<StudentCoachViewModel>
//other html content here

@foreach(var viewModel in Model)
{
   @Html.DisplayFor(o=>o.CoachName)   @Html.DisplayFor(o=>o.StudentName)
}

Now if you are only wanting a single one here rather than a list its even easier

public ActionResult Index(int id)
{
  //code to load model1 and model2 already assumed to be in place. also assuming you loaded this data from a database by the id field being passed into this method.
  return View(new StudentCoachViewModel(){ StudentName = model1.StudentName, CoachName = model2.CoachName});
}

And the view becomes then simply

@model StudentCoachViewModel
//other html here, h1, divs, etc whatever is in your view as html content.
@Html.EditorForModel()
or if you like to display each one instead of the above one line call:
@Html.LabelFor(o=>o.CoachName)
@Html.EditorFor(o=>o.CoachName)

Upvotes: 4

Amit
Amit

Reputation: 15387

try this

public partial class Model3{
   public Model1 model1{get;set;}
   public Model2 model2{get;set;}
}

bind Model3 into view.

Upvotes: 1

vino20
vino20

Reputation: 429

View page can accept only one model, so you can't pass two model at the same time, So you have to Create a another model with all the members of that two model... then in controller, you should convert those two model to one model and pass it to view with help of new model one

or else you can create a another new model with those two model

Upvotes: 0

Related Questions