Link to home
Start Free TrialLog in
Avatar of finance_teacher
finance_teacher

asked on

Asp.net MVC -- System.Collections.Generic.List ERROR

Below worked until I changed it to access my "ViewModel" instead of the regular "Model".

Does anyone know how I can fix ?
-------------------------------------------------------------------------------------------------------------------------------
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MWR.Models.MAINT_WORK_REQ]', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[MWR.Areas.__Testing.ViewModel.MainPageModel]'.
-------------------------------------------------------------------------------------------------------------------------------

Areas/__Testing/ViewModel/MainPageModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MWR.Models;

namespace MWR.Areas.__Testing.ViewModel
{
    public class MainPageModel
    {
        public MAINT_WORK_REQ MR {get; set;}
        public MAINT_WORK_REQ_COMMENTS MRC { get; set; }
    }
}

-------------------------------------------------------------------------------------------------------------------------------
Index.cshtml

@model ListIEnumerable<MWR.Areas.__Testing.ViewModel.MainPageModel>

@foreach (var item in Model)
{
            @Html.DisplayFor(modelItem => item.MR.ID)
            @Html.DisplayFor(modelItem => item.MRC.ID_MWR)

            @Html.DisplayFor(modelItem => item.MRC.ID)
}

-------------------------------------------------------------------------------------------------------------------------------
MAINT_WORK_REQ2Controller.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MWR.Models;
using System.Data.Entity.Validation;
using MoreLinq;
using MWR.Areas.__Testing.DTO;

namespace MWR.Areas.__Testing.Controllers
{
    [Authorize]
    public class MAINT_WORK_REQ2Controller : Controller
    {
        private MVC_testEntities db = new MVC_testEntities();

        public ActionResult Index()
        {
            return View(db.MAINT_WORK_REQ.ToList()); ¿-------------- change to MainPageModel so this LIST matches “index.cshtml” LIST ?
        }
Avatar of kaufmed
kaufmed
Flag of United States of America image

What is ListIEnumerable in "@model ListIEnumerable<MWR.Areas.__Testing.ViewModel.MainPageModel>"?
Avatar of finance_teacher
finance_teacher

ASKER

It is really just the below, but still fails with the same error.

@model List<MWR.Areas.__Testing.ViewModel.MainPageModel>
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Below are the updates, but it still fails.

  Test #1, works = comment out Index.cshtml @foreach
  Test #2, fails = run with Index.cshtml @foreach statement

Does anyone know how I can fix ?
-------------------------------------------------------------------------------------------------------------------------------

Areas/__Testing/ViewModel/MainPageModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MWR.Models;

namespace MWR.Areas.__Testing.ViewModel
{
    public class MainPageModel
    {
        public MAINT_WORK_REQ MR {get; set;}
        public MAINT_WORK_REQ_COMMENTS MRC { get; set; }
    }
}

-------------------------------------------------------------------------------------------------------------------------------
Index.cshtml

@model List<MWR.Areas.__Testing.ViewModel.MainPageModel>

test

@foreach (var item in Model)
{
            @Html.DisplayFor(modelItem => item.MR.ID)
}

-------------------------------------------------------------------------------------------------------------------------------
MAINT_WORK_REQ2Controller.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MWR.Models;
using System.Data.Entity.Validation;
using MoreLinq;
using MWR.Areas.__Testing.DTO;

namespace MWR.Areas.__Testing.Controllers
{
    [Authorize]
    public class MAINT_WORK_REQ2Controller : Controller
    {
        private MVC_testEntities db = new MVC_testEntities();
        private MainPageModel mpm = new MainPageModel();

        public ActionResult Index()
        {
            //return View(db.MAINT_WORK_REQ.ToList());
            return View(mpm.MR);
        }
The Index.cshtml @foreach statement gives an "Object reference not set to an instance of an object." ERROR.