[英]Webgrid paging through generic controller method.
我有一个通用方法来获取应用程序中网格的数据,该方法从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({
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);
网络网格
@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>
我在这里找到了答案: WebGrid分页带有POST数据的松散参数
我只需要捕获该事件,并将?page=[pageNo]
附加到URL,并将发布数据包括在ajax调用的data:
属性中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.