Baggerz
Baggerz

Reputation: 397

MVC Multiple Models in one view using a parent model

I'm trying to add a second model to my index page(contains 2 partial views as a two column page) following the "Passing Multiple Models using ViewModel" section of this page: http://www.codeproject.com/Articles/687061/Using-Multiple-Models-in-a-View-in-ASP-NET-MVC-M

Ive created a model to hold the other 2 called MasterModel which is where i wish to store my other models

public class MasterModel
{
    public UserInfo UserInfo { get; set; }
    public LogDataServerDBEntities LogDataServerDBEntities { get; set; }
}

In my Index, _NewRequest _ExistingRequest pages i changed the model from

@model IEnumerable<TMTMonitorandCompare.Models.UserInfo>

to

@model IEnumerable<TMTMonitorandCompare.Models.MasterModel>

and changed my data display to be "Model.UserInfo"

 @if (Model.UserInfo != null)
 {
   foreach (var item in Model.UserInfo)
      {
          <tr>
             <td>
             <input type="checkbox" class="checks">
             </td>

             <td class="modal2row" data-toggle="modal" data-id="1" data-target="#basicModal3">
                  @Html.DisplayFor(modelItem => item.CreationDateTime)
             </td>

             <td class="modal2row" data-toggle="modal" data-id="1" data-target="#basicModal3">
                 @Html.DisplayFor(modelItem => item.AppModeId)
             </td>

         </tr>
       }
 }

ControllerMethod:

[HttpGet]
    public ActionResult Index(string filtername)
    {
        var filterresults = from m in db.UserInfoes
                            select m;

        filterresults = filterresults.Where(x => x.UserCode.ToString().Contains(filtername)).OrderBy(x => x.UserCode);
        CheckDownloaded();
        PopulateViewbag();

        return View(filterresults);
    }

Only now i get the error :

Error 1 'System.Collections.Generic.IEnumerable' does not contain a definition for 'UserInfo' and no extension method 'UserInfo' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

Can anyone Explain to me where /with what I am going wrong ??

Upvotes: 0

Views: 715

Answers (1)

user3559349
user3559349

Reputation:

UserInfo is an object in MasterModel (not a collection). I suspect what you want is

public class MasterModel
{
  public List<UserInfo> UserInfo { get; set; }
  ....
}

and in the main view

@model TMTMonitorandCompare.Models.MasterModel

then you can use

foreach (var item in Model.UserInfo)
{
  ....

Edit

Based on additional information from OP, the action method needs to be changed to match the model

[HttpGet]
public ActionResult Index(string filtername)
{
  var filterresults = from m in db.UserInfoes select m;
  filterresults = filterresults.Where(x => x.UserCode.ToString().Contains(filtername)).OrderBy(x => x.UserCode);
  ....
  MasterModel model = new MasterModel();
  model.UserInfo = filterresults;
  // set other properties of model as required
  return View(model);
}

Upvotes: 4

Related Questions