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.
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'.
I am bit confused here, as error says it has requesting model type.
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
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?
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>>
What exactly confuses you? In your aspx file you've defined the model as a customer, yet you pass a list instead to it.
Expected model:
System.Web.Mvc.ViewPage<MvcApplication3.Models.Customer>
Your data:
return View(nwd.Customers.ToList());
Obviously a mismatch.
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
@model Customer
then your ActionResult needs to be of type Customer
.
You want to return a list, hence you need to define
@model IEnumerable<Customer>
in the view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With