WebGrid Helper分页不适用于.NET 4.5 MVC 4

时间:2012-11-14 17:18:39

标签: c# asp.net-mvc razor asp.net-mvc-4 webgrid

我已将mvc3项目升级到mvc4项目,我已将目标框架从.NET 4.0更改为.NET 4.5(我不知道是否重要)。

现在,无论我在哪里使用webgrid助手,分页都完全被破坏了。 实际上,更新请求被发送4次,带有“displayClient”作为Id的元素被完全从html文档中删除。

这是我的源代码的几行:

@{
    var grid = new WebGrid(canPage: true, canSort: true, ajaxUpdateCallback: "UpdateWebGridClient", ajaxUpdateContainerId: "displayClient", rowsPerPage: Model.RowPerPageClientId);
    grid.Bind(Model.ClientList, rowCount: Model.ClientNumber, autoSortAndPage: false);
    @grid.GetHtml(
        htmlAttributes: new { id = "grid", @class = "clientTable" },
        rowStyle: "odd",
        alternatingRowStyle: "even",
        footerStyle: "footerWebGrid",
        mode: WebGridPagerModes.All,
        numericLinksCount: 10,
        columns: grid.Columns(
            grid.Column("Id", SUResources.Admin.ClientRef, style: "ref"),
            grid.Column("FullName", SUResources.Admin.ClientFullName, style: "name"),
            grid.Column("Town", SUResources.Admin.ClientTown, style: "town"),
            grid.Column("Country", SUResources.Admin.ClientCountry, style: "country"),
            grid.Column("Email", SUResources.Admin.ClientEmail, style: "email"),
            grid.Column("PurchasedPicture", SUResources.Admin.ClientPurchasedPicture, style: "purchasedPicture"),
            grid.Column("Ca", SUResources.Admin.ClientRevenue, style: "ca"),
            grid.Column(
                style: "first-icon icons",
                format: (item) => Ajax.ActionLink(
                    " ",
                    "ClientInformation",
                    new { clientId = item.Id },
                    new AjaxOptions
                    {
                        HttpMethod = "Get",
                        UpdateTargetId = "showClientInformation",
                        InsertionMode = InsertionMode.Replace
                    },
                    new
                    {
                        @class = "record"
                    }
                )
            ),
            grid.Column(
                style: "icons",
                format:
                @<text>
                    @Html.ActionLink(" ", "Index", "SendMail", new { CustomerId = item.Id }, new { @class = "green-mail" })
                </text>
            ),
            grid.Column(
                style: "icons",
                format:
                @<text>
                    @if (item.StatusId == (short)Shootus.Domain.EnumTypes.CustomerState.Active)
                    {
                        <a onclick="ChangeCustomerStatus(this);" data-customer-id="@item.Id"> <img alt="" title="@SUResources.Admin.ClientEditionActiveUser" src="/Areas/Admin/Content/Images/Icons/ico-client-active-account.png"/></a>
                    }
                    else
                    {
                    string CustomerCurrentStatus = SUResources.Admin.ClientEditionBannedUser;

                    CustomerCurrentStatus = (item.StatusId == (short)Shootus.Domain.EnumTypes.CustomerState.NotValidated) ? SUResources.Admin.ClientEditionEmailNotValidatedUser : CustomerCurrentStatus;
                    CustomerCurrentStatus = (item.StatusId == (short)Shootus.Domain.EnumTypes.CustomerState.TurnedOff) ? SUResources.Admin.ClientEditionUnactiveUser : CustomerCurrentStatus;
                    <a onclick="ChangeCustomerStatus(this);" data-photographer-id="@item.Id"> <img alt="" title="@CustomerCurrentStatus" src="/Areas/Admin/Content/Images/Icons/ico-client-inactive-account.png"/></a>
                    }
                </text>
            ),
            grid.Column(
                style: "icons",
                format: @<text>
                    @Html.ActionLink(" ", "Delete", new { Id = item.Id }, new { @class = "delete" })
                </text>
            )
        )
    );
}
<div class="dot-line">
</div>
<div class="pager client-line-pager">
    @grid.Pager()
</div>

使用css属性显示隐藏webgrid中的默认寻呼机。

我在生成的html中看到,表格中有一些新的属性,如data-swhgajax,data-swhgcallback,以及生成的寻呼机代码:data-swhgcontainer,data-swhglnk,...

我使用jQuery.ajaxPrefilter处理页面中外部表单的一些额外数据。

是否有人遇到同样的问题,他是否解决了这个问题?关于这个问题起源的想法?

感谢您抽出宝贵时间阅读这篇文章,希望有人能帮助我: - (

2 个答案:

答案 0 :(得分:1)

我处理过类似的问题。

在我的情况下,我在AJAX帖子调用后刷新了webgrid内容。并且每次刷新webgrid时,单击标题链接时会发送另一个get请求。 我无法找到原因。

我结束了“ajaxifying”网格的排序:

$("thead tr a").click(function (e) { e.preventDefault(); $.get($(this).attr('href'), function (data) { $('#idGrid').html(data) }) })

以及寻呼机:

$("tfoot tr a").click(function (e) { e.preventDefault(); $.get($(this).attr('href'), function (data) { $('#Grid').html(data) }) })

我撤回了ajaxUpdateContainerId webgrid参数。并修改我的控制器只返回包含webgrid的部分视图是否有一个ajax调用:

if (Request.IsAjaxRequest(){}

我不知道这对任何人都有用......

答案 1 :(得分:0)

升级我的项目之后我遇到了同样的问题,这大量使用了WebGrid,从MVC3到MVC4。我设法解决了我的项目的问题,因此解决方案可能相同/类似

分页/排序不起作用 - 我添加了一个jQuery事件处理程序,它向网格显示了一个ajax微调器,并且不得不改变它。

$("#my-grid a").click(function () { blockGrid($("#my-grid-div")); });

$("#my-grid a").click(function () { blockGrid($("#my-grid-div")); return true; });

即。处理完点击后返回true并修复了分页/排序

链接提交多次 - 默认情况下新网格会使其中的所有链接都启用ajax,这意味着如果您还使用确认对话框,它仍将被提交,如果您选择确定,则会再次提交。在我的情况下,我不得不改变

myGrid.Column(format: (item) => Html.ActionLink(Strings.Remove, "removemember", new { id = ViewBag.GroupId, uid = item.Id }, new { onclick = "return confirmDialog(this, 'Remove Member?', 'Remove Member?')", rel = "remove" }))))

myGrid.Column(format: (item) => Html.ActionLink(Strings.Remove, "removemember", new { id = ViewBag.GroupId, uid = item.Id }, new { data_ajax = "false", onclick = "return confirmDialog(this, 'Remove Member?', 'Remove Member?')", rel = "remove" }))))

即。添加data_ajax = "false"