Webgrid通过通用控制器方法进行分页。

时间:2015-08-19 16:05:11

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

我有一个通用方法来获取我的应用程序中的网格数据,此方法从API获取数据,然后返回填充了所述数据的预定义网格。我的问题是,当我使用这种方法时,寻呼不会起作用 - 即没有任何反应。

我认为可能是分页链接正在调用的URL - 即通用方法 - 而不知道需要发送其他数据。

如何让我的webgrid分页处理这种通用方法?

通用方法

    /// <summary>Generic grid request method. Used to render any grid with data from a defined location.</summary>
    /// <param name="obj">The data required to complete the operation.</param>
    /// <param name="obj[dataurl]">The URL to be used to get the data for the grid.</param>
    /// <param name="obj[data]">Any data that needs to be sent to the data source when getting the data for the grid.</param>
    /// <param name="obj[gridurl]">The URL for the grid to be rendered.</param>
    /// <returns>The grid populated with data in HTML format.</returns>
    [HandleError]
    [HttpPost]
    public ActionResult GridRequest(string obj)
    {            
        JObject Request;
        string DataUrl, GridUrl, RequestData;
        try
        {
            Request = JObject.Parse(obj);
            DataUrl = (string)Request["dataurl"];
            RequestData = Request["data"] != null ? Request["data"].ToString() : null;
            GridUrl = (string)Request["gridurl"];

            HttpClient client = new HttpClient();
            client.Request.ForceBasicAuth = true;
            client.Request.SetBasicAuthentication(C4SMVCApp.Properties.Settings.Default.APIUsername, C4SMVCApp.Properties.Settings.Default.APIPassword);
            HttpResponse response = client.Post(DataUrl, RequestData, HttpContentTypes.ApplicationJson);
            string result = response.RawText;

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("Grid Request Error" + result);
            }
            else
            {
                JArray data = JArray.Parse(result);
                return PartialView(GridUrl, data);
            }
        }
        catch (Exception)
        {                
            throw;
        }
    }

Ajax Call

            $.ajax({
                type: "post",
                url: "/C4S/Home/GridRequest",
                data: {
                    obj: JSON.stringify({
                        dataurl: "{0}Community/AANewsApi/AddToList".format(apiBaseUrl),
                        data: new Object({ listId: listId, items: selectedResult }),
                        gridurl: "~/Areas/Comms/Views/Home/Grids/ListPeopleGrid.cshtml"
                    })
                }
            }).done(function (data) {
                $('#persongrid').replaceWith(data);
                $('#addusercontainer').addClass('hidden');
                listGrid();
            }).fail(failCallback);

的WebGrid

@model IEnumerable<object>
@{
    WebGrid ListPeopleGrid = new WebGrid(
    source: Model,
    ajaxUpdateContainerId: "persongrid",
    canPage: true,
    rowsPerPage: 16);
}
<div id="persongrid">
@ListPeopleGrid.GetHtml(
    tableStyle: "webgrid",
    headerStyle: "webgrid-header color-2",
    footerStyle: "webgrid-footer color-2",
    rowStyle: "webgrid-row",
    alternatingRowStyle: "webgrid-row",
    fillEmptyRows: true,
    nextText: "Next >",
    previousText: "< Prev",
    columns: ListPeopleGrid.Columns(
    ListPeopleGrid.Column("Id", style: "hidden"),
    ListPeopleGrid.Column("Name"),
    ListPeopleGrid.Column("Manager"),
    ListPeopleGrid.Column("AccessLevel"),
    ListPeopleGrid.Column("Site"),
    ListPeopleGrid.Column("Department")
))
</div>

1 个答案:

答案 0 :(得分:0)

我在这里找到了答案:WebGrid pagination loose parameters with POST data

我只需要捕获事件并将?page=[pageNo]附加到URL并将帖子数据包含在ajax调用的data:属性中。