yad4u asp.net mvc model item passed in to dictionary is of type List

asp.net mvc model item passed in to dictionary is of type List

I have a asp.net mvc view which is strongly typed view and i have a controller which returns the ilist user based on the id provided. I am getting the following above error:

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

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Data.User>" %> <% using (Html.BeginForm())  {%> <%= Html.ValidationSummary(true) %>             <fieldset>    <legend>Fields</legend>                                 <div class="editor-label">                     <%= Html.LabelFor(model => model.Id) %>                 </div>                 <div class="editor-field">                     <%= Html.TextBoxFor(model => model.Id) %>                     <%= Html.ValidationMessageFor(model => model.Id) %>                 </div>                                 <div class="editor-label">                     <%= Html.LabelFor(model => model.UserName) %>                 </div>                 <div class="editor-field">                     <%= Html.TextBoxFor(model => model.UserName) %>                     <%= Html.ValidationMessageFor(model => model.UserName) %>                 </div>                  <div class="editor-label">                     <%= Html.LabelFor(model => model.Email) %>                 </div>                 <div class="editor-field">                     <%= Html.TextBoxFor(model => model.Email) %>                     <%= Html.ValidationMessageFor(model => model.Email) %>            public ActionResult EditUser(int id)             {                      var usr = _UsrService.GerUserById(id).ToList<User>();                                     return View(usr);             } 

What is the elegant solution for unrelated views in MVC web frameworks?


Setting width and height of modal window from JavaScript/JQuery to ASP.NET MVC inline code
You can't pass a List to a modal that is to display a single item..
How can I implement a label file for regions that integrates well with MVC application structure?
Try removing the .ToList<User> so that it looks something like.
How asp.net MVC's cache and Firefox's cache work?
Data.User usr = _UsrService.GetUserById(id).FirstOrDefault; return View(usr) 

Is the MVC Controller Layer the ideal place for error handling?

asp.net mvc: read route param inside controller


How can I close one jQuery UI Dialog and focus on another?
You've strongly typed your view class to a single instance of Data.User.

How to Inject HTML on ASP.NET MVC master page
Your action method is attempting to create the view by handing it a List<User>.. Since it looks like you just want to display a single user, try changing your action method to just get one user, like this:.
 public ActionResult EditUser(int id)  {      var usr = _UsrService.GerUserById(id).FirstOrDefault();                     return View(usr);  } 
I'm assuming that the GerUserById(id) method is returning an IEnumerable of some kind, since you're calling the ToList<> method on it.

This means that even if it is a list of one item, you still need to get the object out of it, so calling FirstOrDefault() will give you the first result in the IEnumerable..


Assuming that there is only one user in your system with a given ID (which is sort of the definition of an ID), _UsrService.GerUserById(id) should be changed to return a User rather than a List<User>.

Then your controller can just say:.
return View(_UsrService.GetUserById(id)); 

71 out of 100 based on 16 user ratings 391 reviews