Asp Mvc3网络网格的ajax分页和过滤



我对Asp.Net MVC3的WebGrid控制有点问题。我想要的是使用ajax和webgrid执行搜索视图;像这样的东西:

搜索条件

主题:______

任务类型:_____

提交搜索


具有分页的WebGrid

当我点击搜索按钮时,它通过ajax调用HTTPost操作并应用搜索标准;然而,当我尝试进行分页时,它转到HTTPGet操作;这样,根据搜索条件的过滤器就不会执行。

我把网格放在局部视图中;有一个代码:

    @model IEnumerable<MVC3.Models.Task>
@{
    var grid = new WebGrid(null, rowsPerPage: 2, canPage: true, canSort: true, ajaxUpdateContainerId: "myGrid");
    grid.Bind(Model, rowCount: 3, autoSortAndPage: true);
    grid.Pager(mode: WebGridPagerModes.All);
    @grid.GetHtml(
    columns: grid.Columns(
            grid.Column(format: (item) => Html.ActionLink("Assign Notifications", "AssignNotifications", new { id = item.TaskId })),
            grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.TaskId })),
            grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.TaskId })),
            grid.Column("Subject"),
            grid.Column("Comment"),
            grid.Column(columnName: "Status", header: "Status", format: (item) => item.TaskStatu.Name),
            grid.Column(columnName: "StartDate", header: "Start Date", format: (item) => item.StartDate.ToString("MM/dd/yyy")),
            grid.Column(columnName: "Deadline", header: "Dead Line", format: (item) => item.Deadline.ToString("MM/dd/yyy"))
            )
    );
}

我的索引视图看起来像:

@model MVC3.ViewModel.TaskSearchViewModel
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    Task Index</h2>
<hr />
@using (Ajax.BeginForm(new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "myGrid" }))
{
    <div id="Filters">
        <fieldset>
            <legend>Search criteria</legend>
            <br />
            <table>
                <tr>
                    <td>
                        @Html.LabelFor(x => x.Subject)
                    </td>
                    <td>
                        @Html.TextBoxFor(x => x.Subject, new { style = "width: 255px;" })
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(x => x.TaskTypeId)
                    </td>
                    <td>
                        @Html.DropDownListFor(x => x.TaskTypeId, Model.GetTaskTypesSelectList(), "Select", new { style = "width: 260px;" })
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(x => x.ResponsableId)
                    </td>
                    <td>
                        @Html.DropDownListFor(x => x.ResponsableId, Model.GetResponsablesSelectList(), "Select", new { style = "width: 260px;" })
                    </td>
                </tr>
                <tr>
                    <td>
                        @Html.LabelFor(x => x.StatusId)
                    </td>
                    <td>
                        @Html.DropDownListFor(x => x.StatusId, Model.GetStatusSelectList(), "Select", new { style = "width: 260px;" })
                    </td>
                </tr>
            </table>
        </fieldset>
    </div>
    @Html.Hidden("page")
    <input id="btnSearch" type="submit" value="Search" />
}
<br />
<div id="myGrid">
    @Html.Partial("_TaskSearchResult", Model.ResultTask)
</div>
<br />
@Html.ActionLink("Create new Task", "NewTask")

所以,如果有人知道如何解决我的问题,我将不胜感激。

问候Arturo

网格为每个分页项(和列标题)生成onclick调用,难道不能使用jQuery将默认值替换为对将表单传递回的Ajax方法的调用吗?

我认为这是因为每次单击下一页链接时都要替换网格。也许可以这样使用:

@model IEnumerable<MVC3.Models.Task>
@{
    var grid = new WebGrid(null, rowsPerPage: 2, canPage: true, canSort: true, ajaxUpdateContainerId: "myGrid");
    grid.Bind(Model, rowCount: 3, autoSortAndPage: true);
    grid.Pager(mode: WebGridPagerModes.All);
<div id="myGrid">
    @grid.GetHtml(
    columns: grid.Columns(
            grid.Column(format: (item) => Html.ActionLink("Assign Notifications", "AssignNotifications", new { id = item.TaskId })),
            grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.TaskId })),
            grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.TaskId })),
            grid.Column("Subject"),
            grid.Column("Comment"),
            grid.Column(columnName: "Status", header: "Status", format: (item) => item.TaskStatu.Name),
            grid.Column(columnName: "StartDate", header: "Start Date", format: (item) => item.StartDate.ToString("MM/dd/yyy")),
            grid.Column(columnName: "Deadline", header: "Dead Line", format: (item) => item.Deadline.ToString("MM/dd/yyy"))
            )
    );
</div>
}

最新更新