How to fix The model item passed into the dictionary is of type error?

newday

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]', but this dictionary requires a model item of type 'MvcApplication3.Models.Customer'.]
System.Web.Mvc.ViewDataDictionary
1.SetModel(Object value) +585211
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
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 continuation) +242
System.Web.Mvc.<>c_DisplayClass1c.b_19() +21 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext 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 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.<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 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?

D Stanley

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>>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to fix The model item passed into the dictionary is of type error?

From Dev

The model item passed into the dictionary is of type error

From Dev

The model item passed into the dictionary is of type ..., but this dictionary requires ...

From Java

The model item passed into the dictionary is of type .. but this dictionary requires a model item of type

From Dev

The model item passed into the dictionary is of type ' ', but this dictionary requires a model item of type ' '

From Dev

How to handle The model item passed into dictionary is type of LoginViewModel, but this dictionary requires a model type of RegisterViewModel?

From Dev

The model item passed into the dictionary mismatch error

From Dev

Error : "model item passed into the dictionary is null" in MVC

From Dev

Model items passed to Dictionary is of type error

From Dev

"The model item passed into the dictionary is of type" while passing model to a partial view

From Dev

The model item passed into the dictionary is of type view model but requires generic IEnum

From Dev

"The model item passed into the dictionary is of type" while passing model to a partial view

From Dev

Create ActionResult for save throws error saying The model item passed into the dictionary is of type

From Dev

The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'TestModel'

From Dev

The model item passed into the dictionary is of type 'BlogHomePageModel', but this dictionary requires a model item of type 'BlogHomePageModel'

From Dev

The model item passed into the dictionary is of type 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary requires a model item of type 'X'

From Dev

model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type

From Dev

The model item passed into the dictionary is of type 'a', but this dictionary requires a model item of type 'b'

From Dev

model item passed into the dictionary is of type 'System.Boolean', but this dictionary requires a model item of type 'MyProject.checkboxstate'

From Dev

The model item passed into the dictionary is of type 'System.Net.Http.HttpResponseMessage',

From Dev

The model item passed into the dictionary is of type 'System.Linq.Enumerable

From Dev

The model item passed into the dictionary is of type 'System.Data.Entity.DbSet

From Dev

MVC: The model item passed into the dictionary is of type System.Int32

From Dev

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery', but this dictionary requires a model item of type B

From Dev

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[X]', but this dictionary requires a model item of type 'X'

From Dev

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery', but this dictionary requires a model item of type B

From Dev

The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'IMS.Models.Dealer'

From Dev

ASP.NET MVC 5 The model item passed into the dictionary is of type 'Name.Models.IndexViewModel', but this dictionary requires a model item of type

From Dev

MVC Razor The model item passed into the dictionary is of type List String, but this dictionary requires IEnumerable

Related Related

  1. 1

    How to fix The model item passed into the dictionary is of type error?

  2. 2

    The model item passed into the dictionary is of type error

  3. 3

    The model item passed into the dictionary is of type ..., but this dictionary requires ...

  4. 4

    The model item passed into the dictionary is of type .. but this dictionary requires a model item of type

  5. 5

    The model item passed into the dictionary is of type ' ', but this dictionary requires a model item of type ' '

  6. 6

    How to handle The model item passed into dictionary is type of LoginViewModel, but this dictionary requires a model type of RegisterViewModel?

  7. 7

    The model item passed into the dictionary mismatch error

  8. 8

    Error : "model item passed into the dictionary is null" in MVC

  9. 9

    Model items passed to Dictionary is of type error

  10. 10

    "The model item passed into the dictionary is of type" while passing model to a partial view

  11. 11

    The model item passed into the dictionary is of type view model but requires generic IEnum

  12. 12

    "The model item passed into the dictionary is of type" while passing model to a partial view

  13. 13

    Create ActionResult for save throws error saying The model item passed into the dictionary is of type

  14. 14

    The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'TestModel'

  15. 15

    The model item passed into the dictionary is of type 'BlogHomePageModel', but this dictionary requires a model item of type 'BlogHomePageModel'

  16. 16

    The model item passed into the dictionary is of type 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary requires a model item of type 'X'

  17. 17

    model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type

  18. 18

    The model item passed into the dictionary is of type 'a', but this dictionary requires a model item of type 'b'

  19. 19

    model item passed into the dictionary is of type 'System.Boolean', but this dictionary requires a model item of type 'MyProject.checkboxstate'

  20. 20

    The model item passed into the dictionary is of type 'System.Net.Http.HttpResponseMessage',

  21. 21

    The model item passed into the dictionary is of type 'System.Linq.Enumerable

  22. 22

    The model item passed into the dictionary is of type 'System.Data.Entity.DbSet

  23. 23

    MVC: The model item passed into the dictionary is of type System.Int32

  24. 24

    The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery', but this dictionary requires a model item of type B

  25. 25

    The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[X]', but this dictionary requires a model item of type 'X'

  26. 26

    The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery', but this dictionary requires a model item of type B

  27. 27

    The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'IMS.Models.Dealer'

  28. 28

    ASP.NET MVC 5 The model item passed into the dictionary is of type 'Name.Models.IndexViewModel', but this dictionary requires a model item of type

  29. 29

    MVC Razor The model item passed into the dictionary is of type List String, but this dictionary requires IEnumerable

HotTag

Archive