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.List1[MvcApplication3.Models.Customer]',1.SetModel(Object value) +585211
but this dictionary requires a model item of type
'MvcApplication3.Models.Customer'.]
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(ViewContext1 continuation) +242
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
System.Web.Mvc.<>c_DisplayClass1c.b_19()
+21 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext
controllerContext, IList1 filters, ActionResult actionResult) +1771.End()
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
+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.WrappedAsyncResult1.End() +621.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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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>>
Method 2
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.
Method 3
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.
Method 4
I had this issue and it was a problem of the model of the view. I had this code in my MVC project:
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:
@model ORMVC.Models.Author
So I changed it to:
@model ORMVC.ViewModels.AuthorViewModel
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0