我有一个表,用于保存一些ID以发送请求到Web服务,并保存我的数据以使用IEnumerable viewmodel在视图页面中显示模型并显示数据。现在我想在页面末尾进行分页,因为我有我的存档页面中有100多种产品。现在我该页面如何进行分页?
// CONTROLLER
public ActionResult Archive()
{
var selectspecial = db.ProviderSelections.Where(a =>
a.SpecialSuggestion == true && a.IsActive ==
true).OrderByDescending(a => a.CreateDate).Select(a =>
a.ID).ToList();
var lastitem = selectspecial.Last();
List<string> lst = new List<string>();
for (int i = 0; i < selectspecial.Count; i++)
{
var selectid = selectspecial[i];
lst.Add(selectid.ToString());
}
StringBuilder itemlist = new StringBuilder();
foreach (var item in lst)
{
itemlist.Append(item).Append(",");
}
string id = itemlist.ToString() + lastitem;
WebClient webclient = new WebClient();
webclient.Headers[HttpRequestHeader.ContentType] =
"application/json;charset=utf-8";
string url = "MYWEBSERVICEURL?pids=" + id;
webclient.Headers["Authorization"] = "Basic " +
Convert.ToBase64String(Encoding.Default.GetBytes("USERNAME:PASSWORD"));
webclient.Headers.Add("Request-type", "REQUESTTYPE");
string result = webclient.DownloadString(url);
JObject jObject = JObject.Parse(result);
JToken jUser = jObject["X"];
List<BriefDetailsViewModel> briefdetails = new
List<BriefDetailsViewModel>();
foreach (var item in jUser)
{
BriefDetailsViewModel brief = new BriefDetailsViewModel();
brief.address = (string)item["address"];
//OTHER LINES ARE LIKE ABOVE LINE
briefdetails.Add(brief);
}
return View(briefdetails);
}
// Archive.cshtml
@model IEnumerable<ROOT.BriefDetailsViewModel>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
@foreach (var item in Model)
{
<div class="col-lg-9 col-md-9 col-sm-8 col-xs-8">
<div class="text TextBox">
@item.description
</div>
</div>
//More Data like this....
}
//PAGINATION BOX
</div>
答案 0 :(得分:0)
将分页功能添加到Web服务中的最佳做法如下:
string url = "MYWEBSERVICEURL?pids=" + id + "&pageId="+pageId;
还使用页面大小变量,那么您可以使用LINQ来分页数据:
示例:
int numberOfObjectsPerPage = 10;
var queryResultPage = dataContext.Employees.OrderBy(a=>a.ID)
.Skip(numberOfObjectsPerPage * pageNumber)
.Take(numberOfObjectsPerPage).ToList();
如果您无法更新Web服务,或者您正在使用外部Web服务,则可以先读取数据,然后对其进行分页,然后返回视图:
briefdetails = briefdetails
.Skip(numberOfObjectsPerPage * pageNumber)
.Take(numberOfObjectsPerPage);
return View(briefdetails);
也可以在html中为表格下方的页面进行渲染: //你叫它分页盒
for(int i=1,i<pageNumber;i++)
{
<a href="@Url.Action("YOUR_ACTION",new {pageId = i})">@i<a/>
}