我有一个显示两个下拉列表和一个列出任务的表的视图。我想实现对任务的分页。我使用了以下链接 Using a PagedList with a ViewModel ASP.Net MVC实现分页。
但问题是,我需要在所有3种方法(Index,IndexPost,UpdateTask)中进行分页,并且所有这三种方法都使用相同的视图索引。不幸的是,我只能调用一种方法进行分页。例如,每当我点击UpdateTasks方法结果的第二页链接时,它就会重定向到Index页面。但是它应该保持在同一页面上,但是得到UpdateTasks方法第二页的结果。发生这种情况是因为pagedlistpager中的url操作指向index,并且所有方法都使用相同的索引视图。有人可以帮忙吗?
我需要一种方法来识别它所调用的方法以获得正确的结果,而不是总是指向索引方法
public ActionResult Index(int? categoryId, int? page)
{
int pageNumber = (page ?? 1);
var viewModel = new ProjectViewModel();
if (viewModel.Categories.Any())
{
viewModel.SelectedCategory = categoryId != null ? (int)(categoryId) : viewModel.Categories.First().UniqueId;
GetResults(viewModel, pageNumber);
}
decimal totalPages = ((decimal)(viewModel.Tasks.Count() / (decimal)pageSize));
ViewBag.TotalPages = Math.Ceiling(totalPages);
ViewBag.PageNumber = pageNumber;
return View(viewModel);
}
[HttpPost, ActionName("Index")]
public ActionResult IndexPost(int? categoryId, int? page)
{
int pageNumber = (page ?? 1);
if (categoryId == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var viewModel = new ProjectViewModel();
viewModel.SelectedCategory = (int)categoryId;
if (ModelState.IsValid)
{
GetResults(viewModel, pageNumber);
}
return View(viewModel);
}
private void GetResults(ProjectViewModel viewModel, int pageNumber)
{
viewModel.Projects = this._projectService.Projects(viewModel.SelectedCategory);
if (viewModel.Projects.Any())
{
viewModel.SelectedProject = viewModel.Projects.First().UniqueId;
viewModel.Tasks = this._projectService.Tasks(viewModel.SelectedProject).ToPagedList(pageNumber,pageSize);
}
}
public ActionResult UpdateTasks(int? projectId, int? categoryId, int? page)
{
int pageNumber = (page ?? 1);
if (projectId == null || categoryId == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var viewModel = new ProjectViewModel();
viewModel.SelectedCategory = (int)categoryId;
viewModel.SelectedProject = (int)projectId;
viewModel.Projects = this._projectService.Projects(viewModel.SelectedCategory);
viewModel.Tasks = this._projectService.Tasks(viewModel.SelectedProject).ToPagedList(pageNumber, pageSize);
return View("Index", viewModel);
}
@model ITProjects.ViewModels.ProjectViewModel
@using PagedList.Mvc;
@using PagedList;
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm())
{
<div>
@Html.ActionLink("Create New Project", "CreateProject")
</div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<span>
@if (Model.Categories != null)
{
@Html.DropDownListFor(m => m.SelectedCategory, new SelectList(Model.Categories, "UniqueId", "Name", Model.SelectedCategory), new { @id = "CategoryId", onchange = @"form.action='/Home/Index?categoryId=' +this.value;form.submit();" })
}
@if (Model.Projects != null)
{
@Html.DropDownListFor(m => m.SelectedProject, new SelectList(Model.Projects, "UniqueId", "Name", Model.SelectedProject), new { @id = "ProjectId" })
}
</span>
@Html.ActionLink("View Project", "Project", new { projectId = Model.SelectedProject }, new { @class = "btn btn-default btn-color" })
<p></p>
<p>
@Html.ActionLink("Create New Task", "CreateTask", new { projectId = Model.SelectedProject })
</p>
if (Model.Tasks != null)
{
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Tasks.First().Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Tasks.First().Staff)
</th>
<th>
@Html.DisplayNameFor(model => model.Tasks.First().Status)
</th>
<th>
@Html.DisplayNameFor(model => model.Tasks.First().HeldBy)
</th>
<th>
@Html.DisplayNameFor(model => model.Tasks.First().Progress)
</th>
<th></th>
</tr>
@foreach (var item in Model.Tasks)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Staff)
</td>
<td>
@Html.DisplayFor(modelItem => item.Status)
</td>
<td>
@Html.DisplayFor(modelItem => item.HeldBy)
</td>
<td>
@Html.DisplayFor(modelItem => item.Progress)
</td>
<td>
@Html.ActionLink("Edit", "TaskEdit", new { id = item.UniqueId }) |
@Html.ActionLink("Details", "Details", new { id = item.UniqueId }) |
@Html.ActionLink("Delete", "TaskDelete", new { id = item.UniqueId })
</td>
</tr>
}
</table>
}
@Html.PagedListPager(Model.Tasks, page => Url.Action("Index", new { page }))
}
@section Scripts
{
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$("#ProjectId").change(function () {
var projectId = $('#ProjectId').val();
var categoryId = $('#CategoryId').val();
window.location.href = '@Url.Action("UpdateTasks","Home")' + '?projectId=' + projectId + '&categoryId=' + categoryId;
});
});
</script>