2013-06-12 47 views
0

我在尋呼asyn時遇到問題。我正在使用pagedlist和pagedListMvc包。分頁似乎工作正常,但是當我添加Javascript使其異步沒有發生,即分頁不起作用。我不知道爲什麼這不起作用。請檢閱我的代碼如下尋呼無法正常工作

這裏是控制器代碼

public ActionResult Index(ConsultantSearch model, int page = 1) 
    { const int RecordsPerPage=5; 

     if (!String.IsNullOrEmpty(model.SearchButton)) 
     {     
      var consultants = from con in db.Consultants 
           where (model.ConsultantName == null || con.ConsultantName.Contains(model.ConsultantName)) && (model.CompanyID == null || con.CompanyID == model.CompanyID) 
           && (model.ClientID == null || con.ClientID == model.ClientID) && (model.VendorID == null || con.VendorID == model.VendorID) && (model.RecruiterID == null || con.RecruiterID == model.RecruiterID) 
           && (model.Class == null || con.Class == model.Class) && (model.W2_1099 == null || con.W2_1099 == model.W2_1099) && (model.IsActive == null || con.IsActive == model.IsActive) 
           select con;    


      consultants = consultants.Include(c => c.Client).Include(c => c.Company).Include(c => c.Recruiter).Include(c => c.SalesPerson).Include(c => c.Vendor); 
      return PartialView("_ConsultantList",consultants.ToList().ToPagedList(page,RecordsPerPage)); 


     } 
     else 
     { 

      var consultants = db.Consultants.Include(c => c.Client).Include(c => c.Company).Include(c => c.Recruiter).Include(c => c.SalesPerson).Include(c => c.Vendor); 
      return View(consultants.ToList().ToPagedList(page, RecordsPerPage)); 
     } 
    } 

實現分頁

@model PagedList.IPagedList<ArthurLawrenceKPI.Models.DatabaseModels.Consultant> 
<div id="consultantSearchResults"> 

    <div class"pagedList" data-al-target="#consultantSearchResults"> 
    @Html.PagedListPager(Model, page => Url.Action("Index",new{ page}), 
    PagedListRenderOptions.MinimalWithItemCountText) 
    </div> 
    <table> 
     <tr> 
      <th> 
       @Html.DisplayNameFor(model => model.First().ConsultantName) 
      </th> 
      <th> 
       @Html.DisplayNameFor(model => model.First().EmailAddress) 
      </th> 
      </tr> 
     @foreach (var item in Model) 
     { 
      <tr> 
       <td> 
        @Html.DisplayFor(modelItem => item.ConsultantName) 
       </td> 
       <td> 
        @Html.DisplayFor(modelItem => item.EmailAddress) 
       </td> 
       <td> 
        @Html.ActionLink("Edit", "Edit", new { id = item.ConsultantID }) | 
        @Html.ActionLink("Details", "Details", new { id = item.ConsultantID  }) | 
        @Html.ActionLink("Delete", "Delete", new { id = item.ConsultantID }) 
       </td> 
      </tr> 
     } 
    </table> 

最後這裏是有着一種讓分頁機制非同步的代碼我javascrpt文件中的部分觀點。我將這個js文件和其他相關的.js文件包含在bundle.config中,並將它們包含在_Layout中。

$(function() { 
var getPage = function() { 
    var $a = $(this); 

    var options = { 
     url: $a.attr("href"), 
     data: $("form").serialize(), 
     type: "get" 
    }; 

    $.ajax(options).done(function (data) { 
     var target = $a.parents("div.pagedList").attr("data-otf-target"); 
     $(target).replaceWith(data); 
    }); 
    return false; 

}; 

$(".main-content").on("click", ".pagedList a", getPage); 

});

回答

0

我在我的Javascript代碼中發現錯誤。在以下代碼中有拼寫錯誤

$.ajax(options).done(function (data) { 
    var target = $a.parents("div.pagedList").attr("data-otf-target"); 
    $(target).replaceWith(data); 
}); 
return false; 

「data-otf-target」應該是「data-AL-target」。有下列

$.ajax(options).done(function (data) { 
    var target = $a.parents("div.pagedList").attr("data-AL-target"); 
    $(target).replaceWith(data); 
}); 
return false; 

替換上面的代碼和IF子句中控制器添加條件和寄託都的偉大工程:)

if (!String.IsNullOrEmpty(model.SearchButton) || Request.IsAjaxRequest())