I am trying to run my first ASP.NET MVC application. I created a cotroller and view. Data is taken from Database. However, when project can run but when I try to navigate Customer page I get following error.
我正在尝试运行我的第一个ASP。净MVC应用程序。我创建了一个分配器和视图。数据取自数据库。但是,当项目可以运行时,当我试图浏览客户页面时,会出现以下错误。
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcApplication3.Models.Customer]', but this dictionary requires a model item of type 'MvcApplication3.Models.Customer'.
传入字典的模型项是type 'System.Collections.Generic.List ' 1[MvcApplication3.Models]。但是本字典需要一个“MvcApplication3.Models.Customer”类型的模型项。
I am bit confused here, as error says it has requesting model type.
这里我有点困惑,因为error说它有请求模型类型。
Stack trace is
堆栈跟踪是
Stack Trace:
堆栈跟踪:
[InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List
1[MvcApplication3.Models.Customer]', but this dictionary requires a model item of type 'MvcApplication3.Models.Customer'.]
1.SetModel(Object value) +585211
System.Web.Mvc.ViewDataDictionary
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +371 System.Web.Mvc.ViewPage1.SetViewData(ViewDataDictionary viewData) +48 System.Web.Mvc.WebFormView.RenderViewPage(ViewContext context, ViewPage page) +73
1 continuation) +242
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
System.Web.Mvc.<>c_DisplayClass1c.b_19() +21 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +177
1.End() +57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
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
System.Web.Mvc.<>c_DisplayClass1d.b_18(IAsyncResult asyncResult) +14
System.Web.Mvc.Async.<>c_DisplayClass4.b_3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult1.End() +62
1.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
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 result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514812 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155[InvalidOperationException]:传入字典的模型项属于“System.Collections.Generic.List1 . MvcApplication3.Models”。但是本字典需要一个“MvcApplication3.Models.Customer”类型的模型项。System.Web.Mvc.ViewDataDictionary1。SetModel(对象值)+ 585211 System.Web.Mvc.ViewDataDictionary . .男星(ViewDataDictionary字典)+ 371 System.Web.Mvc.ViewPage1。SetViewData(ViewDataDictionary视讯系统)+ 48 System.Web.Mvc.WebFormView。RenderViewPage(ViewContext context, ViewPage page) +73 System.Web.Mvc.WebFormView。RenderView(ViewContext ViewContext ViewContext、TextWriter writer、对象实例)+38 System.Web.Mvc.BuildManagerCompiledView。渲染(ViewContext ViewContext ViewContext, TextWriter writer) +115 System.Web.Mvc.ViewResultBase。+ 295 System.Web.Mvc.ControllerActionInvoker ExecuteResult(ControllerContext上下文)。
c__DisplayClass1a.< invokeactionresultwithfilter >b__17() +23 System.Web.Mvc.ControllerActionInvoker。InvokeActionResultFilter(IResultFilter, ResultExecutingContext preContext, Func1 continuation) +242 System.Web.Mvc.<>c_DisplayClass1c.b_19() +21 System.Web.Mvc.ControllerActionInvoker。invokeactionresultfilter (ControllerContext ControllerContext ControllerContext, IList1过滤器,ActionResult) +177 system . web . web . mvc . async .<>c__DisplayClass2a. b__20() +89 system . web.bbvc . async .< bb2 c__DisplayClass25。[endnoteref: 1] [endnoteref: 1] [endnoteref: 1] [endnoteref: 1] [endnoteref: 1] [endnoteref: 1]]。EndInvokeAction(IAsyncResult asyncResult)+ 43 System.Web.Mvc。< > c_DisplayClass1d。b_18(IAsyncResult asyncResult)+ 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。 b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult1.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.asyncresult ' 1.End() +62 System.Web.Mvc.MvcHandler。EndProcessRequest(IAsyncResult asyncResult)+ 47 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler。EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514812 System.Web.HttpApplication。ExecuteStep(IExecutionStep, boolean&completedsync) +155。
Here is my controller code.
这是我的控制器代码。
namespace MvcApplication3.Controllers
{
public class CustomerController : Controller
{
//
// GET: /Customer/
public ActionResult Index()
{
Models.NorthwindDataContext nwd = new Models.NorthwindDataContext();
return View(nwd.Customers.ToList());
}
}
}
Here is the view
这是视图
<%@ 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>
Can anybody give me a hint to fix it?
有人能给我点提示吗?
5 个解决方案
#1
7
You're trying to pass a collection to a view that's designed for a single object.
您试图将集合传递给为单个对象设计的视图。
change your view declaration to
将视图声明更改为。
Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication3.Models.Customer>>
继承= " System.Web.Mvc.ViewPage < IEnumerable < MvcApplication3.Models.Customer > >
#2
4
What exactly confuses you? In your aspx file you've defined the model as a customer, yet you pass a list instead to it.
混淆了你什么?在您的aspx文件中,您已经将模型定义为客户,但是您将一个列表传递给它。
Expected model:
预计模型:
System.Web.Mvc.ViewPage<MvcApplication3.Models.Customer>
Your data:
你的数据:
return View(nwd.Customers.ToList());
Obviously a mismatch.
显然不匹配。
#3
1
In your related view (i.e. the .cshtml
file) look for the @model
declaration: the ActionResult
needs to return the right instance of the model class, e.g. if you have defined
在您的相关视图(即.cshtml文件)中查找@model声明:ActionResult需要返回模型类的正确实例,例如,如果您已经定义了
@model Customer
then your ActionResult needs to be of type Customer
. You want to return a list, hence you need to define
那么您的ActionResult需要是Customer类型。您希望返回一个列表,因此需要定义
@model IEnumerable<Customer>
in the view.
在视图中。
#4
0
The model item passed into the dictionary is of type
传入字典的模型项是类型的
System.Collections.Generic.List1[MvcApplication3.Models.Customer]
but this dictionary requires a model item of type
但是这本字典需要一个类型的模型项
MvcApplication3.Models.Customer
Type mismatch.
类型不匹配。
#5
0
I had this issue and it was a problem of the model of the view. I had this code in my MVC project:
我有这个问题,它是视图模型的问题。我在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);
but the View Form.cshtml had the wrong model:
但是视图的形式。cshtml有错误的模型:
@model ORMVC.Models.Author
So I changed it to:
所以我把它改成:
@model ORMVC.ViewModels.AuthorViewModel
#1
7
You're trying to pass a collection to a view that's designed for a single object.
您试图将集合传递给为单个对象设计的视图。
change your view declaration to
将视图声明更改为。
Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication3.Models.Customer>>
继承= " System.Web.Mvc.ViewPage < IEnumerable < MvcApplication3.Models.Customer > >
#2
4
What exactly confuses you? In your aspx file you've defined the model as a customer, yet you pass a list instead to it.
混淆了你什么?在您的aspx文件中,您已经将模型定义为客户,但是您将一个列表传递给它。
Expected model:
预计模型:
System.Web.Mvc.ViewPage<MvcApplication3.Models.Customer>
Your data:
你的数据:
return View(nwd.Customers.ToList());
Obviously a mismatch.
显然不匹配。
#3
1
In your related view (i.e. the .cshtml
file) look for the @model
declaration: the ActionResult
needs to return the right instance of the model class, e.g. if you have defined
在您的相关视图(即.cshtml文件)中查找@model声明:ActionResult需要返回模型类的正确实例,例如,如果您已经定义了
@model Customer
then your ActionResult needs to be of type Customer
. You want to return a list, hence you need to define
那么您的ActionResult需要是Customer类型。您希望返回一个列表,因此需要定义
@model IEnumerable<Customer>
in the view.
在视图中。
#4
0
The model item passed into the dictionary is of type
传入字典的模型项是类型的
System.Collections.Generic.List1[MvcApplication3.Models.Customer]
but this dictionary requires a model item of type
但是这本字典需要一个类型的模型项
MvcApplication3.Models.Customer
Type mismatch.
类型不匹配。
#5
0
I had this issue and it was a problem of the model of the view. I had this code in my MVC project:
我有这个问题,它是视图模型的问题。我在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);
but the View Form.cshtml had the wrong model:
但是视图的形式。cshtml有错误的模型:
@model ORMVC.Models.Author
So I changed it to:
所以我把它改成:
@model ORMVC.ViewModels.AuthorViewModel