Weird InvalidOperationException error RRS feed

  • Question

  • Hello, I'm creating a project with VS2017 using .NET Core MVC web app template.

    I added an EF controller with CRUD operations to manage users in a wine-related web app and generated the corresponding Views.

    So my problem is:

    An unhandled exception occurred while processing the request.

    InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[HajaBinho.Models.Utilizador]', but this ViewDataDictionary instance requires a model item of type 'HajaBinho.Models.Utilizador'.

    Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary.EnsureCompatible(object value)

    The app can´t list the users already in the Database. I send the info from the controller to the view like this:

    public async Task<IActionResult> Index()
                return View(await _context.Utilizador.ToListAsync());

    and the View receives an IEnumerable:

    @model IEnumerable<HajaBinho.Models.Utilizador>
        ViewData["Title"= "Index";

    Am I not sending a List to the view dictionary?

    Also, this code was generated by Visual studio I don´t know why it breaks. It was working before.

    Please help, cheers.

    • Moved by CoolDadTx Thursday, December 27, 2018 6:49 PM ASP.NET related
    Thursday, December 27, 2018 2:32 PM

All replies

  • Your view and controller code looks fine, Clean and Rebuild the project and try again.

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 27, 2018 3:34 PM
  • I did that and nothing changed. Also, this project is hosted/shared through GitLab and my friend does not have this problem. We are using the same branch which is updated for us both. he can access the Index view but I can´t. So weird
    Thursday, December 27, 2018 4:23 PM
  • Please post questions related to MVC in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, December 27, 2018 6:49 PM