views:

55

answers:

3

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);
            }
+1  A: 

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)
rockinthesixstring
It still gives the same error
Pinu
see my edit....
rockinthesixstring
also, i changed the example to say `GetUserById` instead of `GerUserById` since it looked like you had a typo in your original post.
rockinthesixstring
A: 

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.

womp
A: 

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