【问题标题】:How to fix The model item passed into the dictionary is of type error?如何修复传入字典的模型项类型错误?
【发布时间】:2014-02-24 07:52:25
【问题描述】:

我正在尝试运行我的第一个 ASP.NET MVC 应用程序。我创建了一个控制器和视图。数据取自数据库。但是,当项目可以运行但当我尝试导航客户页面时,我收到以下错误。

传入字典的模型项是类型 'System.Collections.Generic.List`1[MvcApplication3.Models.Customer]', 但是这本字典需要一个类型的模型项 'MvcApplication3.Models.Customer'。

我在这里有点困惑,因为错误说它有请求模型类型。

堆栈跟踪是

堆栈跟踪:

[InvalidOperationException: 传入字典的模型项 是类型 'System.Collections.Generic.List1[MvcApplication3.Models.Customer]', but this dictionary requires a model item of type 'MvcApplication3.Models.Customer'.]
System.Web.Mvc.ViewDataDictionary
1.SetModel(对象值) +585211
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary 字典) +371 System.Web.Mvc.ViewPage1.SetViewData(ViewDataDictionary viewData) +48 System.Web.Mvc.WebFormView.RenderViewPage(ViewContext context, ViewPage page) +73
System.Web.Mvc.WebFormView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +38
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +295 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func
1 续)+242
System.Web.Mvc.c_DisplayClass1c.b_19() +21 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext 控制器上下文,IList1 filters, ActionResult actionResult) +177
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +89 System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
System.Web.Mvc.c_DisplayClass1d.b_18(IAsyncResult 异步结果)+14
System.Web.Mvc.Async.c_DisplayClass4.b_3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult1.End() +62
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult
1.End() +62
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.c_DisplayClass8.b_3(IAsyncResult asyncResult) +25
System.Web.Mvc.Async.c_DisplayClass4.b_3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult 结果)+9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514812 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

这是我的控制器代码。

namespace MvcApplication3.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            Models.NorthwindDataContext nwd = new Models.NorthwindDataContext();
            return View(nwd.Customers.ToList());
        }

    }
}

这里是风景

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication3.Models.Customer>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>

</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="FeaturedContent" runat="server">
</asp:Content>

<asp:Content ID="Content4" ContentPlaceHolderID="ScriptsSection" runat="server">
</asp:Content>

谁能给我一个提示来解决它?

【问题讨论】:

  • 您的索引操作返回客户列表。您的视图是强类型的客户列表还是客户的单个实例?
  • 在我看来您正在传递 List 但有 @model 客户,而不是视图上的列表
  • 只是看看这是否有效,将nwd.Customers.ToList()替换为nwd.Customers.ToList().FirstOrDefault()

标签: c# asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

我遇到了这个问题,这是视图模型的问题。我的 MVC 项目中有这段代码:

var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Author, AuthorViewModel>();
        });

        IMapper mapper = config.CreateMapper();
        var dest = mapper.Map<Author, AuthorViewModel>(author);

        return View("Form", dest);

但 View Form.cshtml 的模型有误:

@model ORMVC.Models.Author

所以我把它改成了:

@model ORMVC.ViewModels.AuthorViewModel

【讨论】:

    【解决方案2】:

    在您的相关视图(即.cshtml 文件)中查找@model 声明:ActionResult 需要返回模型类的正确实例,例如如果你已经定义了

    @model Customer
    

    那么你的 ActionResult 需要是 Customer 类型。 您想返回一个列表,因此您需要定义

    @model IEnumerable<Customer>
    

    在视图中。

    【讨论】:

      【解决方案3】:

      您正在尝试将集合传递给为单个对象设计的视图。

      将您的视图声明更改为

      Inherits="System.Web.Mvc.ViewPage&lt;IEnumerable&lt;MvcApplication3.Models.Customer&gt;&gt;

      【讨论】:

      • 出于兴趣。在使用RenderPartial,并且第二个参数是null 的用法中,执行部分是否会从调用页面假定模型?
      【解决方案4】:

      究竟是什么让你感到困惑?在您的 aspx 文件中,您已将模型定义为客户,但您将一个列表传递给它。

      预期型号:

      System.Web.Mvc.ViewPage<MvcApplication3.Models.Customer>
      

      您的数据:

      return View(nwd.Customers.ToList());
      

      显然不匹配。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-21
        • 1970-01-01
        • 2011-11-26
        • 1970-01-01
        • 2015-02-02
        • 2011-09-06
        • 2019-06-01
        相关资源
        最近更新 更多