0

I have a view which displays two dropdowns and a table listing out tasks. I want to implement paging to the tasks. I have used the following link Using a PagedList with a ViewModel ASP.Net MVC to implement paging.

But the problem here is , I require paging in all the 3 methods (Index, IndexPost, UpdateTask) and all three methods uses the same view Index. And unfortunately, I can only call one method for paging. For example, whenever I click on the second page link on the results of UpdateTasks method, it is redirecting to Index page. It should however stay on the same page but get the results of second page of UpdateTasks method. This is happening because the url action in the pagedlistpager is pointing to index and the same index view is used for all the methods. Could anyone please help with this?

I need a way to identify the method it's called from to get the right results and not always pointing to index method

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>
ChinnaR
  • 797
  • 3
  • 9
  • 24
  • Either you can use WebGrid instead of a plain html table, you don't need entity framework to use WebGrid, or you can control pagination manually with querystring handling the page pagination and handling everything manually, this could help https://stackoverflow.com/questions/109232/what-is-the-best-way-to-paginate-results-in-sql-server/109290#109290 – Jorge F Aug 11 '17 at 16:43
  • I could able to implement the paging but there is another problem as mentioned in the post. I have just modified it. There are 3 methods using the same view and I can only point PagedListPager to point to one view alone. I need a way to identify the method it's called from to get the right results and not always pointing to index method – ChinnaR Aug 11 '17 at 16:53

0 Answers0