How to get an object from the webgrid.

1.6k views Asked by At

I've got a MVC webgrid which i'm populating with user objects.

@model IEnumerable<UserSearchDisplayModel>    
<div class="user-search-list">
        @{
            var grid = new WebGrid(Model, defaultSort: "UserName", rowsPerPage: 25, selectionFieldName:"SelectedRow");
            }
       @grid.GetHtml(
       tableStyle: "webgrid-table",
       headerStyle: "webgrid-header",
       footerStyle: "webgrid-footer",
       alternatingRowStyle: "webgrid-alternating-row",
       selectedRowStyle: "webgrid-selected-row",
       rowStyle: "webgrid-row-style",
       mode: WebGridPagerModes.All,
       columns: grid.Columns(
       grid.Column("Edit", header: null, format:@<text>@item.GetSelectLink("Edit")</text>),
       grid.Column("Active", header: "Active", format:@<text><input name="checkbox" type="checkbox" value="@item.Active" disabled="disabled" @(item.Active==true?"Checked":null)/></text>, style:"webgrid-userActive"),
       grid.Column("UserName", header: "User Name", format:@<text>@item.UserName</text>, style:"webgrid-userName"),
       grid.Column("FirstName", header: "First Name", format:@<text>@item.FirstName</text>, style:"webgrid-userFirstName"),
       grid.Column("LastName", header: "Last Name", format:@<text>@item.LastName</text>, style:"webgrid-userLastName"),
       grid.Column("TeamName", header: "Team", format:@<text>@item.TeamName</text>, style:"webgrid-userTeamName"),
       grid.Column("DateLastLogin", "Date of last login", format:(item) =>string.Format("{0:dd-MM-yyyy}",item.DateLastLogin??""), style:"webgrid-userDateLastLogin"),
       grid.Column("PasswordChanged", "Password changed", format:(item) =>string.Format("{0:dd-MM-yyyy}",item.PasswordChanged??""), style:"webgrid-userPasswordChanged")
       ))
 </div>

I then have the following code which should display a partial view when a user is selected so that their details can be edited.

<div class="user-details" id="AdminUserDetails">
    @if (grid.HasSelection)
    {
        UserSearchDisplayModel user = grid.Rows[grid.SelectedIndex].Value;
        @RenderPage("~/Views/Admin/Users/_AdminUserDetails.cshtml", user)
    }
</div>

And the partial view is expecting a single object of

@model UserSearchDisplayModel 

But when a row is selected the user variable always looks ok but at the point where this is being passed to the partial view I get the error

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

This to me means that the user variable is actually an IEnumerable object but with a breakpoint on it's only shows me one object of my UserSearchDisplayModel class. Can anyone shed some light on how to get round this?

1

There are 1 answers

0
Adam Bielecki On BEST ANSWER

Instead of using @RenderPage try

    UserSearchDisplayModel user = grid.Rows[grid.SelectedIndex].Value;
    @Html.Partial("_AdminUserDetails", user)

Works for me.