如何从webgrid获取对象。

时间:2014-11-18 17:08:14

标签: c# asp.net-mvc model-view-controller webgrid

我有一个MVC webgrid,我填充了用户对象。

@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>

然后,我有以下代码,在选择用户时应显示部分视图,以便可以编辑其详细信息。

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

部分视图期待

的单个对象
@model UserSearchDisplayModel 

但是当选择一行时,用户变量总是看起来不错,但是在传递到局部视图的那一刻我得到了错误

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'.

这对我来说意味着用户变量实际上是一个IEnumerable对象,但是它上面有一个断点,它只显示了我的UserSearchDisplayModel类的一个对象。任何人都可以阐明如何绕过这个?

1 个答案:

答案 0 :(得分:0)

而不是使用@RenderPage尝试

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

适合我。