2012-02-10 22 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で呼び出す: http://archive.msdn.microsoft.com/mag201107WebGrid/Release/ProjectReleases.aspx?ReleaseId=5667 これは実際にはフィルタリングには問題ありませんが、フィルタリングされた結果が得られたら、絞り込まれた検索結果の並べ替え順序を変更しようとすると要素が失われますWebGridが最初のコントローラーアクションを再度実行するため、 'name'パラメーターの値が返されます。おそらくこれは最善のアプローチではありませんが、私が言ったように、私はWebGridを使ったことがないので、私は喜んで学びます。どんな助けでも本当に感謝しています。ありがとう。

+0

私はこの回答と思います:http://stackoverflow.com/a/10052663/1651536あなたの問題を解決します – eryel

答えて

5

フォームにFormMethod.Postを追加して、リクエストが2番目のActionResultに移動するようにしてください。それ以外の場合、リクエストはGETであり、パラメータを指定せずに最初のActionResult Index()に移動します。

関連する問題