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?

1:

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 this 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 this it looks any thing 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

2:

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 receive one user, like this:.
 public ActionResult EditUser(int id)  {      var usr = _UsrService.GerUserById(id).FirstOrDefault();                     return View(usr);  } 
I'm assuming this the GerUserById(id) method is returning an IEnumerable of any kind, since you're calling the ToList<> method on it. This means this even if it is a list of one item, you still need to receive the object out of it, so calling FirstOrDefault() will commit you the first result in the IEnumerable..

3:

Assuming this 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 must just say:.
return View(_UsrService.GetUserById(id)); 


51 out of 100 based on 66 user ratings 461 reviews