0

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 ??

3
  • could you please post the controller code, and show how you are constructing the model object(MasterModel) Commented Sep 19, 2014 at 10:55
  • Not sure this may be an issue, but as a practice,it is not adviceable to use the same class name as property name [public UserInfo UserInfo { get; set; }] Commented Sep 19, 2014 at 10:57
  • 1
    @Baggerz, I have rolled back your edit - you cant change the question completely. Just add the code I requested in the comments Commented Sep 19, 2014 at 11:10

1 Answer 1

4

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);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the assistance Stephan, after making the change above the errors in visual studio disappear. I also changed the partial views to have the same @model statement, but I then get this error when running the solution: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[TMTMonitorandCompare.Models.UserInfo]', but this dictionary requires a model item of type 'TMTMonitorandCompare.Models.MasterModel'.
Edit your question to include the controller method - you don't appear to be passing the correct object to the view - it should be something like MasterModel model = new MasterModel(); model.UserInfo = someCollection; return View(model);
I don't have that in my code, Ive posed my index method above.
I'll update my answer to address the additional problem
@Baggerz, Sorry about that. IEnumerable is fine in your case, although if you wanted List (e.g. to use a for loop) you could have added .ToList() to the end of the query.
|

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.