开发者

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

开发者 https://www.devze.com 2023-01-06 09:41 出处:网络
I have a asp.net mvc view which is strongly typedviewand i have a controller which returns the ilist user based on the id provided. I am getting the following above error:

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.Begi开发者_运维技巧nForm()) 
{%> <%= 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);
            }


You can't pass a List to a modal that is to display a single item.

Try removing the .ToList<User> so that it looks something like

Data.User usr = _UsrService.GetUserById(id).FirstOrDefault;
return View(usr)


You've strongly typed your view class to a single instance of Data.User. 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));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号