尝试运行我的应用程序时出现以下错误:
'传递到字典中的模型项的类型为'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]'。
“
这是堆栈跟踪:
[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
这是我的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();
}
}
}
}
答案 0 :(得分:4)
问题在于您有一个控制器操作,它将List<Blog>
实例传递给强类型为IEnumerable<BlogDataEntities>
的视图,这是不可能的。
所以你有一个像这样的控制器动作:
public ActionResult Foo()
{
List<Blog> blogs = ...
return View(blogs);
}
和相应的观点:
@model IEnumerable<BlogDataEntities>
...
发生此错误的另一种可能性是您的视图键入控制器操作传递的正确模型,但在此视图中,您尝试呈现键入不同模型的部分:
@model List<Blog>
...
@Html.Partial("_MyPartial")
_MyPartial.cshtml
看起来像这样:
@model IEnumerable<BlogDataEntities>
...
再一次是不可能的。在这种情况下,您可以使用Html.Partial
帮助器的第二个参数为此部分提供正确的模型。