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.Generi c.List`1[M WR.Models. MAINT_WORK _REQ]', but this dictionary requires a model item of type 'System.Collections.Generi c.List`1[M WR.Areas._ _Testing.V iewModel.M ainPageMod el]'.
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -
Areas/__Testing/ViewModel/ MainPageMo del.cs
using System;
using System.Collections.Generic ;
using System.Linq;
using System.Web;
using MWR.Models;
namespace MWR.Areas.__Testing.ViewMo del
{
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. MainPageMo del>
@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.Validat ion;
using MoreLinq;
using MWR.Areas.__Testing.DTO;
namespace MWR.Areas.__Testing.Contro llers
{
[Authorize]
public class MAINT_WORK_REQ2Controller : Controller
{
private MVC_testEntities db = new MVC_testEntities();
public ActionResult Index()
{
return View(db.MAINT_WORK_REQ.ToL ist()); ¿-------------- change to MainPageModel so this LIST matches “index.cshtml” LIST ?
}
Does anyone know how I can fix ?
--------------------------
The model item passed into the dictionary is of type 'System.Collections.Generi
--------------------------
Areas/__Testing/ViewModel/
using System;
using System.Collections.Generic
using System.Linq;
using System.Web;
using MWR.Models;
namespace MWR.Areas.__Testing.ViewMo
{
public class MainPageModel
{
public MAINT_WORK_REQ MR {get; set;}
public MAINT_WORK_REQ_COMMENTS MRC { get; set; }
}
}
--------------------------
Index.cshtml
@model ListIEnumerable<MWR.Areas.
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem
@Html.DisplayFor(modelItem
@Html.DisplayFor(modelItem
}
--------------------------
MAINT_WORK_REQ2Controller.
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.Validat
using MoreLinq;
using MWR.Areas.__Testing.DTO;
namespace MWR.Areas.__Testing.Contro
{
[Authorize]
public class MAINT_WORK_REQ2Controller : Controller
{
private MVC_testEntities db = new MVC_testEntities();
public ActionResult Index()
{
return View(db.MAINT_WORK_REQ.ToL
}
What is ListIEnumerable in "@model ListIEnumerable<MWR.Areas. __Testing. ViewModel. MainPageMo del>"?
ASKER
It is really just the below, but still fails with the same error.
@model List<MWR.Areas.__Testing.V iewModel.M ainPageMod el>
@model List<MWR.Areas.__Testing.V
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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/ MainPageMo del.cs
using System;
using System.Collections.Generic ;
using System.Linq;
using System.Web;
using MWR.Models;
namespace MWR.Areas.__Testing.ViewMo del
{
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.V iewModel.M ainPageMod el>
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.Validat ion;
using MoreLinq;
using MWR.Areas.__Testing.DTO;
namespace MWR.Areas.__Testing.Contro llers
{
[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.ToL ist());
return View(mpm.MR);
}
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/
using System;
using System.Collections.Generic
using System.Linq;
using System.Web;
using MWR.Models;
namespace MWR.Areas.__Testing.ViewMo
{
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.V
test
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem
}
--------------------------
MAINT_WORK_REQ2Controller.
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.Validat
using MoreLinq;
using MWR.Areas.__Testing.DTO;
namespace MWR.Areas.__Testing.Contro
{
[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.ToL
return View(mpm.MR);
}
ASKER
The Index.cshtml @foreach statement gives an "Object reference not set to an instance of an object." ERROR.