new*_*day 14 c# asp.net asp.net-mvc asp.net-mvc-4
我正在尝试运行我的第一个ASP.NET MVC应用程序.我创建了一个cotroller和视图.数据来自数据库.但是,当项目可以运行但是当我尝试浏览客户页面时,我会收到以下错误.
传递到字典中的模型项的类型为'System.Collections.Generic.List`1 [MvcApplication3.Models.Customer]',但此字典需要类型为"MvcApplication3.Models.Customer"的模型项.
我在这里有点困惑,因为错误说它已经请求了模型类型.
堆栈跟踪是
堆栈跟踪:
[InvalidOperationException:传递到字典中的模型项的类型为'System.Collections.Generic.List 1.SetModel(Object value)+585211 System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary)+371 System.Web. Mvc.ViewPage 1 continuation)+242 System.Web.Mvc.<> c_ DisplayClass1c.b _19()+21 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext,IList 1.End()+ 57 System.Web. Mvc.Async.AsyncControllerActionInvoker.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.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_ DisplayCl ass4.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
1[MvcApplication3.Models.Customer]', but this dictionary requires a model item of type 'MvcApplication3.Models.Customer'.]
System.Web.Mvc.ViewDataDictionary1.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, Func1 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.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
这是我的控制器代码.
namespace MvcApplication3.Controllers
{
public class CustomerController : Controller
{
//
// GET: /Customer/
public ActionResult Index()
{
Models.NorthwindDataContext nwd = new Models.NorthwindDataContext();
return View(nwd.Customers.ToList());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是观点
<%@ 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>
Run Code Online (Sandbox Code Playgroud)
任何人都可以给我一个解决方法吗?
您正在尝试将集合传递给专为单个对象设计的视图.
将您的视图声明更改为
Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication3.Models.Customer>>
| 归档时间: |
|
| 查看次数: |
80084 次 |
| 最近记录: |