2016-05-12 36 views
0

我在ASP.NET MVC,我正在顯示數據從我的數據庫到我的屏幕。我在頁面頂部有一個下拉列表,根據2個選項(選項1和選項2)過濾數據。爲什麼分頁導致我的DropDown列表過濾器重置?

我已經添加了AJAX來加載PartialView,它將根據過濾器更新屏幕上的數據。

我的問題是,每當我改變頁面分頁,我的過濾器被重置爲第一個選項,選項1

例如,頁面加載,我切換下拉列表過濾器選項2。選項2數據正確顯示在屏幕上。我嘗試通過分頁轉到第2頁。數據顯示第2頁,但過濾器已切換回選項1,現在顯示選項1數據(它應該保留在選項2上,我應該看到選項2的數據)。

下面是我的圖和局部景觀:

查看

//Dropdown list with 2 options to filter data 
@Html.DropDownList("Filter", new SelectList(Model.comboBoxFilerValues), new { @id = "filterDropdown" }) 

//Everything in this div gets reloaded by AJAX with the partial view 
<div id="reportTable"> 
<table id="lifeUsage"> 
    <thead> 
     <tr> 
      //print table header 
      @foreach (var column in Model.columnHeaders) columns 
      { 
       <td> 
        @column 
       </td> 
      } 
     </tr> 
    </thead> 
     //print data with paged list for pagination 
     @foreach (System.Data.DataRow row in Model.plist) 
     { 
      <tr> 
       @foreach (var cell in row.ItemArray) 
       { 
        <td>@cell</td> 
       } 
      </tr> 

     } 
</table> 
//pagination page control 
@Html.PagedListPager(Model.plist, page => Url.Action("Index", new { page })) 
</div> 


@section scripts 
{ 
<script> 
    $(document).ready(function() { 

     $('#filterDropdown').change(function() { 

      var filter = $('#filterDropdown').val();//value selected from droplist list 

      $.ajax({ 
       type: "GET", 
       url: "/Home/FilterReport", 
       data: { 
        filterValue: filter 
        } 
      }).done(function (partialViewResult) { 
       $('#reportTable').html(partialViewResult);//load new partial view with filtered data 


      }); 
     }); 
    }); 
</script> 
} 

管窺

<table id="lifeUsage"> 
<thead> 
    <tr> 
     @foreach (var column in Model.columnHeaders) 
     { 
      <td> 
       @column 
      </td> 
     } 
    </tr> 
</thead> 

@foreach (System.Data.DataRow row in Model.plist) 
{ 
    <tr> 
     @foreach (var cell in row.ItemArray) 
     { 
      <td>@cell</td> 
     } 
    </tr> 

} 
</table> 
@Html.PagedListPager(Model.plist, page => Url.Action("Index", new { page })) 

任何人都可以看到,爲什麼我的下拉列表過濾器被重置選項1,每當我切換頁面?

編輯

控制器

public class HomeController : Controller 
{ 
    QueryService data = new QueryService("connection"); 

    public ActionResult Index(string filter, int? page) 
    { 
     DataTableModel lifeUsageReport = new DataTableModel(); 
     lifeUsageReport.columnHeaders = new List<string>(); 
     lifeUsageReport.list = data.GetLifeUsageReport(filter).Tables[0].AsEnumerable().ToList(); 
     lifeUsageReport.plist = new PagedList<DataRow>(lifeUsageReport.list, page ?? 1, 10); 
     lifeUsageReport.comboBoxFilerValues = new List<string>(); 
     foreach(DataColumn column in data.GetLifeUsageReport(filter).Tables[0].Columns) 
     { 
      lifeUsageReport.columnHeaders.Add(column.Caption); 
     } 
     foreach(DataRow row in data.GetLifeUsageReport(filter).Tables[1].Rows) 
     { 
      lifeUsageReport.comboBoxFilerValues.Add(row.ItemArray[0].ToString()); 
     } 
     return View(lifeUsageReport); 
    } 

    [HttpGet] 
    public ActionResult FilterReport(string filterValue, int? page) 
    { 
     List<string> headers = new List<string>(); 

     foreach (DataColumn column in data.GetLifeUsageReport(filterValue).Tables[0].Columns) 
     { 
      headers.Add(column.Caption); 
     } 


     FilteredReportViewModel model = new FilteredReportViewModel() 
     { 
      plist = new PagedList<DataRow> (data.GetLifeUsageReport(filterValue).Tables[0].AsEnumerable().ToList(), page ?? 1, 10), 
      columnHeaders = new List<string>(headers) 

     }; 

     return PartialView("_FilteredReportPartialView",model); 
    } 
+0

請分享您的控制器代碼。 – JCM

+0

@JCM添加了控制器 – Reeggiie

回答

0

@Reeggiie:事情是,你在分頁呼喚你的索引操作,因此你會一遍又一遍回你的整個看法。你需要做的是檢查是否有Ajax請求。

在您的索引操作檢查它是否是一個Ajax請求與否:

if (Request.IsAjaxRequest()) 
{ 
    return PartialView("_FilteredReportPartialView", model); 
} 

希望它能幫助。

+0

這個@ Ajax.PageLinks會替換我的@ Html.PagedListPager嗎? – Reeggiie

+0

此外,我的視圖不認可@ Ajax中的PageLinks() – Reeggiie

+0

@ Ajax.PageLinks是一種自定義的分頁方式,我將編輯我的答案,嘗試只檢查「if(Request.IsAjaxRequest()) 「返回您的PartialView或不。 – JCM

相關問題