Solved

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

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

Expert Comment

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

Author Comment

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

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

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
The Index.cshtml @foreach statement gives an "Object reference not set to an instance of an object." ERROR.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now