Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-12-19
5
Medium Priority
?
54 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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