Using paging in partial view, asp.net mvc

前端 未结 3 714
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 01:11

I\'d greatly appreciate if someone could advise on following: In my view I display the list of items:

@model PagedList.IPagedList
@using PagedLi         


        
3条回答
  •  死守一世寂寞
    2020-12-13 01:38

    My project setup: MVC4, MvcPaging (PagedList), with Areas. This answer is geared towards that setup.

    Short Answer:
    While configuring your AjaxOptions for the pager, ensure you set your area (properly)

    new AjaxOptions { UpdateTargetId = "grid-list" , OnBegin = "beginPaging", 
                      OnSuccess = "successPaging", OnFailure = "failurePaging"}
            , new { area = "Admin", controller = "MyController", action = "Index"}
    

    Long Answer:
    The PagedList sample shows how to pass to an area and do paging within an area, but they do not show a sample of how to use paging with a Partial View.

    The sample project has the following code (taken from _AjaxEmployeeList.cshtml):

    }, new AjaxOptions
                {
                    UpdateTargetId = "grid-list",
                    OnBegin = "beginPaging",
                    OnSuccess = "successPaging",
                    OnFailure = "failurePaging"
                }, 
        new { controller = "Home", action = "Index", 
            employee_name = ViewData["employee_name"] }))
    

    The PagedList sample uses an inline table inside a foreach, so you do not have any problems/conflicts with this setup.

    @using MvcPaging
    @model IPagedList
    
            @foreach (var item in Model)
            { 
                
            }    
    
    @item.ID

    While refactoring this table into a partial view (to encapsulate logic (and to deal with paging), I started to get a "The partial view '_MyPartialView' was not found or no view engine supports the searched locations"

    @using MvcPaging
    @using MyProject.Models
    @model IPagedList
      foreach (var item in Model)
            {
            
    @Html.Partial("_Detail", item)
    }

    I went through a ration of changes attempting to force the area into the Html.Partial() call, including:
    Modifying how routing is handled, which didn't work.
    How to set a Default Route (To an Area) in MVC
    Fully qualifying the path to the partial view, which did work but was ugly.
    mvc3 - using partial views in a different area
    The research into getting the partial views to load showed me how the MVC engine processes requests. This sent me back to modifying the Ajax Pager, to send the area with each request. I tried to use a similar syntax as the Html.ActionLink. (This didn't/doesn't work)

    @Html.ActionLink("Admin", "Index", "Admin", new { area = "Admin" }, null)
    

    That didn't work,so failing all else, I mirrored the pattern for setting the controller, which leads us to:

    new AjaxOptions { UpdateTargetId = "grid-list" ,  OnBegin = "beginPaging", 
                      OnSuccess = "successPaging", OnFailure = "failurePaging"}
               , new { area = "Admin", controller = "MyController", action = "Index"}
    

    My personal lesson from this: C# != Razor != Javascript. Each does things slightly differently and you have to be sure you know which language the line your writing is for.

提交回复
热议问题