Weird InvalidOperationException error related to ViewDataDictionary

Multi tool use
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. I'm trying to list all the users in the database (table Utilizador) and I'm using EF via DBContext Class.
When I pass the list of users from the database to the index View I get this error:
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'.
I've searched for related answers but nothing helped me mostly because the code was written by developers, mine was generated by Visual Studio.
I've already tried creating another UsersController and generating new Views to no avail.
Clean and Rebuild the solution didn't do anything.
Here is the code generated by Visual Studio (which is not working right now but worked before):
UtilizadoresController.cs
namespace HajaBinho.Controllers
{
public class UtilizadoresController : Controller
{
private readonly BinhoBDContext _context;
public UtilizadoresController(BinhoBDContext context)
{
_context = context;
}
// GET: Utilizadores
public async Task<IActionResult> Index()
{
return View(await _context.Utilizador.ToListAsync());
}
//......
Index View, Index.cshtml:
@model IEnumerable<HajaBinho.Models.Utilizador>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Username)
</th>
//.....
c# asp.net-core-mvc entity-framework-core
|
show 1 more comment
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. I'm trying to list all the users in the database (table Utilizador) and I'm using EF via DBContext Class.
When I pass the list of users from the database to the index View I get this error:
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'.
I've searched for related answers but nothing helped me mostly because the code was written by developers, mine was generated by Visual Studio.
I've already tried creating another UsersController and generating new Views to no avail.
Clean and Rebuild the solution didn't do anything.
Here is the code generated by Visual Studio (which is not working right now but worked before):
UtilizadoresController.cs
namespace HajaBinho.Controllers
{
public class UtilizadoresController : Controller
{
private readonly BinhoBDContext _context;
public UtilizadoresController(BinhoBDContext context)
{
_context = context;
}
// GET: Utilizadores
public async Task<IActionResult> Index()
{
return View(await _context.Utilizador.ToListAsync());
}
//......
Index View, Index.cshtml:
@model IEnumerable<HajaBinho.Models.Utilizador>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Username)
</th>
//.....
c# asp.net-core-mvc entity-framework-core
Could you please clarify what you don't understand about error message? You may also inspect code posted in the question to see what model is passed to view and what model view expects...
– Alexei Levenkov
Dec 27 '18 at 17:24
1
I don´t understand why it says that the ViewDataDictionary instance requires a singleHajaBinho.Models.Utilizador
object when the Index view clearly receives anIEnumerable<HajaBinho.Models.Utilizador>
; The index view would list all users in the database so of course, it should receive a collection of them.
– Ferro
Dec 27 '18 at 17:31
2
Make absolutely sure you're looking at the right view. Change the model type in the view and see if the error message changes accordingly. Conversely, you can also change the controller so it gives the view a singleUtilizador
, then observe which view was actually used. When things don't make sense, challenge what you think you know. Most likely, one of your assumptions is invalid.
– Amy
Dec 27 '18 at 17:37
Need more coffee - somehow readmodel IEnumerable<HajaBinho.Models.Utilizador>
asmodel HajaBinho.Models.Utilizador
... Anyway just search all your views and see if any takesHajaBinho.Models.Utilizador
as model if approach suggested by @Amy does not work.
– Alexei Levenkov
Dec 27 '18 at 17:42
Thanks for the help guys I found the problem. I had@model HajaBinho.Models.Utilizador
in the View _layout.cshtml which is loaded before all other views. I did this hoping to encapsulate user.username and user.password fields in a popup form hidden by default. I'll just use simple text fields for these.
– Ferro
Dec 27 '18 at 18:38
|
show 1 more comment
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. I'm trying to list all the users in the database (table Utilizador) and I'm using EF via DBContext Class.
When I pass the list of users from the database to the index View I get this error:
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'.
I've searched for related answers but nothing helped me mostly because the code was written by developers, mine was generated by Visual Studio.
I've already tried creating another UsersController and generating new Views to no avail.
Clean and Rebuild the solution didn't do anything.
Here is the code generated by Visual Studio (which is not working right now but worked before):
UtilizadoresController.cs
namespace HajaBinho.Controllers
{
public class UtilizadoresController : Controller
{
private readonly BinhoBDContext _context;
public UtilizadoresController(BinhoBDContext context)
{
_context = context;
}
// GET: Utilizadores
public async Task<IActionResult> Index()
{
return View(await _context.Utilizador.ToListAsync());
}
//......
Index View, Index.cshtml:
@model IEnumerable<HajaBinho.Models.Utilizador>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Username)
</th>
//.....
c# asp.net-core-mvc entity-framework-core
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. I'm trying to list all the users in the database (table Utilizador) and I'm using EF via DBContext Class.
When I pass the list of users from the database to the index View I get this error:
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'.
I've searched for related answers but nothing helped me mostly because the code was written by developers, mine was generated by Visual Studio.
I've already tried creating another UsersController and generating new Views to no avail.
Clean and Rebuild the solution didn't do anything.
Here is the code generated by Visual Studio (which is not working right now but worked before):
UtilizadoresController.cs
namespace HajaBinho.Controllers
{
public class UtilizadoresController : Controller
{
private readonly BinhoBDContext _context;
public UtilizadoresController(BinhoBDContext context)
{
_context = context;
}
// GET: Utilizadores
public async Task<IActionResult> Index()
{
return View(await _context.Utilizador.ToListAsync());
}
//......
Index View, Index.cshtml:
@model IEnumerable<HajaBinho.Models.Utilizador>
@{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Username)
</th>
//.....
c# asp.net-core-mvc entity-framework-core
c# asp.net-core-mvc entity-framework-core
edited Dec 27 '18 at 17:25
Alexei Levenkov
83.9k890132
83.9k890132
asked Dec 27 '18 at 16:50


Ferro
187
187
Could you please clarify what you don't understand about error message? You may also inspect code posted in the question to see what model is passed to view and what model view expects...
– Alexei Levenkov
Dec 27 '18 at 17:24
1
I don´t understand why it says that the ViewDataDictionary instance requires a singleHajaBinho.Models.Utilizador
object when the Index view clearly receives anIEnumerable<HajaBinho.Models.Utilizador>
; The index view would list all users in the database so of course, it should receive a collection of them.
– Ferro
Dec 27 '18 at 17:31
2
Make absolutely sure you're looking at the right view. Change the model type in the view and see if the error message changes accordingly. Conversely, you can also change the controller so it gives the view a singleUtilizador
, then observe which view was actually used. When things don't make sense, challenge what you think you know. Most likely, one of your assumptions is invalid.
– Amy
Dec 27 '18 at 17:37
Need more coffee - somehow readmodel IEnumerable<HajaBinho.Models.Utilizador>
asmodel HajaBinho.Models.Utilizador
... Anyway just search all your views and see if any takesHajaBinho.Models.Utilizador
as model if approach suggested by @Amy does not work.
– Alexei Levenkov
Dec 27 '18 at 17:42
Thanks for the help guys I found the problem. I had@model HajaBinho.Models.Utilizador
in the View _layout.cshtml which is loaded before all other views. I did this hoping to encapsulate user.username and user.password fields in a popup form hidden by default. I'll just use simple text fields for these.
– Ferro
Dec 27 '18 at 18:38
|
show 1 more comment
Could you please clarify what you don't understand about error message? You may also inspect code posted in the question to see what model is passed to view and what model view expects...
– Alexei Levenkov
Dec 27 '18 at 17:24
1
I don´t understand why it says that the ViewDataDictionary instance requires a singleHajaBinho.Models.Utilizador
object when the Index view clearly receives anIEnumerable<HajaBinho.Models.Utilizador>
; The index view would list all users in the database so of course, it should receive a collection of them.
– Ferro
Dec 27 '18 at 17:31
2
Make absolutely sure you're looking at the right view. Change the model type in the view and see if the error message changes accordingly. Conversely, you can also change the controller so it gives the view a singleUtilizador
, then observe which view was actually used. When things don't make sense, challenge what you think you know. Most likely, one of your assumptions is invalid.
– Amy
Dec 27 '18 at 17:37
Need more coffee - somehow readmodel IEnumerable<HajaBinho.Models.Utilizador>
asmodel HajaBinho.Models.Utilizador
... Anyway just search all your views and see if any takesHajaBinho.Models.Utilizador
as model if approach suggested by @Amy does not work.
– Alexei Levenkov
Dec 27 '18 at 17:42
Thanks for the help guys I found the problem. I had@model HajaBinho.Models.Utilizador
in the View _layout.cshtml which is loaded before all other views. I did this hoping to encapsulate user.username and user.password fields in a popup form hidden by default. I'll just use simple text fields for these.
– Ferro
Dec 27 '18 at 18:38
Could you please clarify what you don't understand about error message? You may also inspect code posted in the question to see what model is passed to view and what model view expects...
– Alexei Levenkov
Dec 27 '18 at 17:24
Could you please clarify what you don't understand about error message? You may also inspect code posted in the question to see what model is passed to view and what model view expects...
– Alexei Levenkov
Dec 27 '18 at 17:24
1
1
I don´t understand why it says that the ViewDataDictionary instance requires a single
HajaBinho.Models.Utilizador
object when the Index view clearly receives an IEnumerable<HajaBinho.Models.Utilizador>
; The index view would list all users in the database so of course, it should receive a collection of them.– Ferro
Dec 27 '18 at 17:31
I don´t understand why it says that the ViewDataDictionary instance requires a single
HajaBinho.Models.Utilizador
object when the Index view clearly receives an IEnumerable<HajaBinho.Models.Utilizador>
; The index view would list all users in the database so of course, it should receive a collection of them.– Ferro
Dec 27 '18 at 17:31
2
2
Make absolutely sure you're looking at the right view. Change the model type in the view and see if the error message changes accordingly. Conversely, you can also change the controller so it gives the view a single
Utilizador
, then observe which view was actually used. When things don't make sense, challenge what you think you know. Most likely, one of your assumptions is invalid.– Amy
Dec 27 '18 at 17:37
Make absolutely sure you're looking at the right view. Change the model type in the view and see if the error message changes accordingly. Conversely, you can also change the controller so it gives the view a single
Utilizador
, then observe which view was actually used. When things don't make sense, challenge what you think you know. Most likely, one of your assumptions is invalid.– Amy
Dec 27 '18 at 17:37
Need more coffee - somehow read
model IEnumerable<HajaBinho.Models.Utilizador>
as model HajaBinho.Models.Utilizador
... Anyway just search all your views and see if any takes HajaBinho.Models.Utilizador
as model if approach suggested by @Amy does not work.– Alexei Levenkov
Dec 27 '18 at 17:42
Need more coffee - somehow read
model IEnumerable<HajaBinho.Models.Utilizador>
as model HajaBinho.Models.Utilizador
... Anyway just search all your views and see if any takes HajaBinho.Models.Utilizador
as model if approach suggested by @Amy does not work.– Alexei Levenkov
Dec 27 '18 at 17:42
Thanks for the help guys I found the problem. I had
@model HajaBinho.Models.Utilizador
in the View _layout.cshtml which is loaded before all other views. I did this hoping to encapsulate user.username and user.password fields in a popup form hidden by default. I'll just use simple text fields for these.– Ferro
Dec 27 '18 at 18:38
Thanks for the help guys I found the problem. I had
@model HajaBinho.Models.Utilizador
in the View _layout.cshtml which is loaded before all other views. I did this hoping to encapsulate user.username and user.password fields in a popup form hidden by default. I'll just use simple text fields for these.– Ferro
Dec 27 '18 at 18:38
|
show 1 more comment
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53948313%2fweird-invalidoperationexception-error-related-to-viewdatadictionary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53948313%2fweird-invalidoperationexception-error-related-to-viewdatadictionary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1c5p p1QOyDFFmTswUjMY,FLXm83xP3pkLb CTFa6nE,i1rB f,G,x,Ta,Lk8L5aWmFHCC WcKlgbkUrk51cnZgeZPRylIgVdx,GypAQF,SGR
Could you please clarify what you don't understand about error message? You may also inspect code posted in the question to see what model is passed to view and what model view expects...
– Alexei Levenkov
Dec 27 '18 at 17:24
1
I don´t understand why it says that the ViewDataDictionary instance requires a single
HajaBinho.Models.Utilizador
object when the Index view clearly receives anIEnumerable<HajaBinho.Models.Utilizador>
; The index view would list all users in the database so of course, it should receive a collection of them.– Ferro
Dec 27 '18 at 17:31
2
Make absolutely sure you're looking at the right view. Change the model type in the view and see if the error message changes accordingly. Conversely, you can also change the controller so it gives the view a single
Utilizador
, then observe which view was actually used. When things don't make sense, challenge what you think you know. Most likely, one of your assumptions is invalid.– Amy
Dec 27 '18 at 17:37
Need more coffee - somehow read
model IEnumerable<HajaBinho.Models.Utilizador>
asmodel HajaBinho.Models.Utilizador
... Anyway just search all your views and see if any takesHajaBinho.Models.Utilizador
as model if approach suggested by @Amy does not work.– Alexei Levenkov
Dec 27 '18 at 17:42
Thanks for the help guys I found the problem. I had
@model HajaBinho.Models.Utilizador
in the View _layout.cshtml which is loaded before all other views. I did this hoping to encapsulate user.username and user.password fields in a popup form hidden by default. I'll just use simple text fields for these.– Ferro
Dec 27 '18 at 18:38