4

I tried finding a similar question, but none of them match to my scenario. So I am posting this as a new question. My Model and Controller looks like the following:

Model:

public class table1
{
     public string name1 { get; set; }
}

public class table2
{
     public string name2 { get; set; }
}

public class bothTable
{
     public table1 table1 { get; set; }
     public table2 table2 { get; set; }
}

Controller:

List<table1> list1 = //generate list from table1
List<table2> list2 = //generate list from table2

var model = ?

return View(model);

I just need a way to pass both the output list to the model so I can pass it on to the View. Obviously I have more than 1 property in both table1 and table2, and list1 and list2 are generated from LINQ so they have multiple results.

Can anyone please help me on what my model should look like so that I can pass both lists to my View?

I was refering to this link but it doesn't match exactly to my scenario: http://forums.asp.net/t/1998033.aspx?Passing+multiple+Models+to+View+from+Controller

I have also refereed to other similar questions in stackoverflow but I couldn't get the desired result.

2
  • create a third model combining the results you want and bind that model to your view Commented Apr 27, 2016 at 14:28
  • I realize this question is a couple years old, but I put out a proper answer for it below. it took a few hours to fess out the full solution. Commented Apr 28, 2019 at 21:28

3 Answers 3

8

You should create a ViewModel that contains all the information you need in the view.

public class MyViewModel {
   public List<table1> TheFirstTable {get;set;}
   public List<table2> TheSecondTable {get;set;}
}

You can fill it like this

MyViewModel viewModel = new MyViewModel();
viewModel.TheFirstTable = //first table content
viewModel.TheSecondTable = //second table content
return View(viewmodel);

On the view you can then retreive the information from the model like

Model.TheFirstTable

or use it in a foreach loop

@foreach(var row in Model.TheFirstTable) {
    //do something with the row
}
Sign up to request clarification or add additional context in comments.

2 Comments

Model.TheFirstTable is giving me an error in the View saying MyViewModel doesn't contain a definition for TheFirstTable. At the top of the page, I have @model List<MyProject.Models.MyViewModel>
You don't need to put the MyViewModel in a list. Those lists are inside the viewmodel. So it should be @model MyProject.Models.MyViewModel
2

Change your model for bothTable to be List<table1> List<table2>. For example:

public class table1
{
     public string name1 { get; set; }
}

public class table2
{
     public string name2 { get; set; }
}

public class bothTable
{
     public List<table1> table1 { get; set; }
     public List<table2> table2 { get; set; }
}

Then, generate your model:

List<table1> list1 = //generate list from table1
List<table2> list2 = //generate list from table2

var model = new bothTable { table1 = list1, table2 = list2 };

return View(model);

Comments

0

Proper wrapper to make this work is

public class ListData
{
    public IEnumerable<EntityFrameworkModel1> List1 { get; set; }
    public IEnumerable<EntityFrameworkModel2> List2 { get; set; }
}

Then the Controller passes values to the view as follows..

var dbtables = new OtherContext();
string userId = User.Identity.GetUserId().ToString();

ListData listdata = new ListData();
var list1 = dbtables.EntityFrameworkModel1.ToList()
            .Where(c => c.OwnerId == userId && c.Active == true)
            .OrderBy(c => c.DateAdded).Reverse();

ListData.List1 = list1;

.. return View(listdata);

MVC 5.2 EntityFramework 6.0 VS 2017

Then the view model can be referenced as..

@model App.Models.ListData

@foreach (var record in Model.List1)
{
    <div class="form-row">
        <div class="Info1">
            @record.Field1 [@record.Field2]
            @record.Field3
        </div>
        <hr />
    </div>
}

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.