2012-02-10 55 views
4

基本上,我使用WebGrid,我需要過濾結果。我在這裏遇到的第一個問題是這是我第一次使用WebGrid,我希望你們中的一些人能夠幫助我......到目前爲止,我已經設法對網格結果進行排序並使用Ajax進行過濾,但是,重新排序篩選結果,子集丟失,我回到開始的全套結果。我知道它爲什麼會發生,但我不知道如何使它工作。ASP.NET MVC3:WebGrid + Ajax過濾器+ Ajax排序和尋呼

例子:

在我的觀點:

@model IQueryable<Cities> 
@section MoreScripts 
{ 
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> 
} 

@using (Ajax.BeginForm(new AjaxOptions { InsertionMode = InsertionMode.Replace,  UpdateTargetId = "GridData"})) 
{ 
    <fieldset> 
     <legend>Search Filters</legend> 
     <br /> 
     <div> 
      Name 
     </div> 
     <div> 
      @Html.TextBox("Name") 
     </div> 
     <p> 
      <input type="submit" value="Search" /> 
     </p> 
    </fieldset> 
} 

<div id="GridData"> 
    @Html.Partial("Grid", Model) 
</div> 

我的局部視圖:

@model IQueryable<Cities> 

@{ 
    var grid = new WebGrid<Cities>(null,rowsPerPage: 5, defaultSort: "Nombre", ajaxUpdateContainerId: "GridData"); 
    grid.Bind(Model, autoSortAndPage: true, rowCount: Model.Count()); 
    @grid.GetHtml(columns: 
        grid.Columns(
           grid.Column("Name", "Name", canSort: true), 
           grid.Column("CreationDate", "Creation Date", canSort: true), 
           grid.Column("Active", "Active", canSort: true, format: @<text><input type="checkbox" disabled="disabled" value="@item.ID" @(item.Active == true ? "Checked" : null) /></text>), 
                      grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "editLink smallCell", @title = "Edit" })), 
           grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { @class = "deleteLink smallCell", @title = "Delete" }))), 
          tableStyle: "webgrid", 
          headerStyle: "webgrid-header", 
          footerStyle: "webgrid-footer", 
          alternatingRowStyle: "webgrid-alternating-row", 
          selectedRowStyle: "webgrid-selected-row", 
          rowStyle: "webgrid-row-style");  
} 

最後有什麼做錯了就在這裏,我的控制器上:

public ActionResult Index() 
    { 
     return View(repository.GetAllRecords().OrderByDescending(f => f.CreationDate)); 
    } 

    [HttpPost] 
    public ActionResult Index(string name) 
    { 
     var data = repository.GetAllRecords(); 

     if(!string.IsNullOrEmpty(name)) 
      data = data.Where(a => a.Name.Contains(name)); 

     data = data.OrderByDescending(f => f.CreationDate); 

     return PartialView("Grid", data); 
    } 

我也是你唱一類的WebGrid:的WebGrid如下所示: http://archive.msdn.microsoft.com/mag201107WebGrid/Release/ProjectReleases.aspx?ReleaseId=5667

所以,這其實工作正常進行過濾,但一旦你得到的篩選結果,然後嘗試改變你縮小搜索結果的排序順序,你失去的元素和'name'參數的值,因爲WebGrid會再次執行第一個控制器操作。也許這不是最好的方法,但正如我所說,我從來沒有使用WebGrid,所以我願意學習。任何幫助將非常感激。謝謝。

+0

我想這個答案:http://stackoverflow.com/a/10052663/1651536解決您的問題 – eryel 2012-09-11 09:50:51

回答

5

嘗試將FormMethod.Post添加到您的表單中,以便請求將轉到第二個ActionResult。否則,請求是GET,並且不帶參數進入第一個ActionResult Index()。