Solved

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

Posted on 2013-12-19
5
35 Views
Last Modified: 2015-12-08
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 ?
        }
0
Comment
Question by:finance_teacher
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39730499
What is ListIEnumerable in "@model ListIEnumerable<MWR.Areas.__Testing.ViewModel.MainPageModel>"?
0
 

Author Comment

by:finance_teacher
ID: 39730637
It is really just the below, but still fails with the same error.

@model List<MWR.Areas.__Testing.ViewModel.MainPageModel>
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39730659
Well, you database code doesn't return a MainPageModel, it returns an IEnumerable<MAINT_WORK_REQ>, which is what you are passing to the view. If you want to keep going with this view model, then you need to create a new instance of it in your controller and pass this new instance to the view.

However, your viewmodel doesn't have a property that can accept IEnumerable<MAINT_WORK_REQ>, so something needs to be reworked.
0
 

Author Comment

by:finance_teacher
ID: 39731663
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);
        }
0
 

Author Comment

by:finance_teacher
ID: 39731667
The Index.cshtml @foreach statement gives an "Object reference not set to an instance of an object." ERROR.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Signal R -send data to client: only if they are allowed to see it ? 1 52
Simple Injector with Web Service 4 41
C# Gridview 1 33
No Data for DropDown List 2 26
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question