Reputation: 797
I get the following error when trying to run my application:
'The model item passed into the dictionary is of type 'System.Collections.Generic.List1[MvcApplication2.Models.Blog]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[MvcApplication2.Models.BlogDataEntities]'.
'
Here is the stack trace:
[InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcApplication2.Models.Blog]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcApplication2.Models.BlogDataEntities]'.]
System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +324899
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +377
System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +48
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +99
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
System.Web.Mvc.Controller.ExecuteCore() +116
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8969117
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
And here is my BlogController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class BlogController : Controller
{
//
// GET: /Blog/
public ActionResult Index()
{
using (var db = new BlogDataEntities())
{
return View(db.Blogs.ToList());
}
}
//
// GET: /Blog/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Blog/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Blog/Create
[HttpPost]
public ActionResult Create(Blog blog)
{
try
{
using (var db = new BlogDataEntities())
{
db.Blogs.Add(blog);
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Blog/Edit/5
public ActionResult Edit(int id)
{
using (var db = new BlogDataEntities())
{
return View(db.Blogs.Find(id));
}
}
//
// POST: /Blog/Edit/5
[HttpPost]
public ActionResult Edit(int id, Blog blog)
{
try
{
using (var db = new BlogDataEntities())
{
db.Entry(blog).State = System.Data.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch
{
return View();
}
}
//
// GET: /Blog/Delete/5
public ActionResult Delete(int id)
{
using (var db = new BlogDataEntities())
{
return View(db.Blogs.Find(id));
}
}
[HttpPost]
public ActionResult Delete(int id, Blog blog)
{
try
{
using (var db = new BlogDataEntities())
{
db.Entry(blog).State = System.Data.EntityState.Deleted;
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
Upvotes: 2
Views: 10754
Reputation: 1038830
The problem is that you have a controller action which passes a List<Blog>
instance to a view which is strongly typed to IEnumerable<BlogDataEntities>
which is not possible.
So you have a controller action like this:
public ActionResult Foo()
{
List<Blog> blogs = ...
return View(blogs);
}
and the corresponding view:
@model IEnumerable<BlogDataEntities>
...
Another possibility for this error occurring is that your view is typed to the correct model that the controller action passed but inside this view you tried to render a partial which is typed to a different model:
@model List<Blog>
...
@Html.Partial("_MyPartial")
where _MyPartial.cshtml
looks like this:
@model IEnumerable<BlogDataEntities>
...
which once again is not possible. In this case you could use the second argument to the Html.Partial
helper to provide a correct model for this partial.
Upvotes: 4