0

I have used entity data model wizard to create an entity framework for my tables in the SQL server however I need to use more than two models in my razor view within MVC, currently I have an existing linq query in :

Controller View

var test = from a in db.tbl_users
       where a == 2
       select a;

return view (test.ToList());

Razor View:

@model IEnumerable <Telephone_Search.Models.tbl_users>

@foreach (var item in model)

@HTML.DisplayFor(modelItem => item.users)

However I plan to access other tables which exist in the Entity Framework which I want loop through, something like this

@model IEnumerable <Telephone_Search.Models.'EntityFramework'>

@foreach (var item in 'entityframework')

@HTML.DisplayFor(modelItem => EntityFramework.'table'.'item')

I have tried to create a view modal however my classes are separated within the model folder and I'm not sure how to wrap these classes into one view model?

ekad
  • 14,436
  • 26
  • 44
  • 46
Aqib Mehrban
  • 71
  • 2
  • 9
  • Possible duplicate of [Two models in one view in ASP MVC 3](http://stackoverflow.com/questions/5550627/two-models-in-one-view-in-asp-mvc-3) – markpsmith Oct 22 '15 at 11:52
  • My models are in partial classes produced by the Entity Data Model Wizard , I need to find a method of placing a parent view model with these partial classes. – Aqib Mehrban Oct 22 '15 at 11:56

2 Answers2

5

You have to create a new class, usually called a ViewModel and pass the data in that class:

public class MyViewModel
{
  public IEnumerable<Telephone_Search.Models.tbl_users> users;
  public IEnumerable<OtherType> otherThings;
  public string SomeOtherProp {get;set;}
}

In the view:

@model MyViewModel

foreach (var usr in Model.users) {...}
foreach (var ot in Model.OtherThings) {...}
<span>@Model.SomeOtherProp</span>

In the controller:

var test = from a in db.tbl_users
       where a == 2
       select a;

var otherTypes = from x in db.tbl_otherTypes where x.Prop > 10 select x;

return this.View(new MyViewModel
{
  users = test,
  otherThings = otherTypes,
  SomeOtherProp = "stackoverflow answer"
});
Santhos
  • 3,348
  • 5
  • 30
  • 48
  • I gladly did so. However, do not use comments for thanking people. It is enough that you mark it as correct and upvote it. Taken from SO help: "Use comments to ask for more information or clarify a question or answer." ;) – Santhos Oct 22 '15 at 12:18
  • Nevermind , I pointed at users instead of tbl_users in the razor. Working finally :) thank you! – Aqib Mehrban Oct 22 '15 at 13:34
0

as directly we can not bind multiple model but there are other options to work with multiple models.

  1. using Viewbag you can pass data of other modal

     ViewBag.Users = db.Students.ToList();
    

    in View Side

     @{
         var students = (List<Students>)ViewBag.Students;
      }
    
  2. You can create Modal with properties contains object of other modal

     public class StudentViewModel
     {
          public string StudentName{get;set;}
          public IEnumerable<MarksheetViewModels> Results;          
     }
    
Lalji Dhameliya
  • 1,729
  • 1
  • 17
  • 26