Solved

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

Posted on 2013-12-19
5
37 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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