Pinu
Pinu

Reputation: 7520

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);
            }

Upvotes: 0

Views: 906

Answers (3)

StriplingWarrior
StriplingWarrior

Reputation: 156748

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));

Upvotes: 0

womp
womp

Reputation: 116987

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.

Upvotes: 0

Chase Florell
Chase Florell

Reputation: 47437

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)

Upvotes: 1

Related Questions