2016-11-21 70 views
0

在對C#Web應用程序進行維護時,遇到MVC Grid。 我的觀點之一有4個面板,每個面板都有一個包含大量數據的網格。出於性能原因,我希望能夠使用AJAX分別更新每個面板。mvc grid ajax,網格已更新,但部分視圖不是

在我的第一次嘗試中,我(重新)使用AJAX手動加載面板。我注意到網格失去了它的javascript功能。

所以在我的第二次嘗試中,我安裝了Grid.Mvc.Ajax。 現在網格已更新,其功能仍然完好無損,但遇到以下問題,無法輕鬆解決:

網格已更新,但其周圍面板未更新,而您實際上必須指定包含要更新的網格的(部分)視圖。所述AjaxGrid.cs包含此方法:

public string ToJson(string gridPartialViewName, Controller controller) 
    { 
     var htmlHelper = new KlaHtmlHelpers(); 
     return htmlHelper.RenderPartialViewToString(gridPartialViewName, this, controller); 
    } 

爲了目視證明局部沒有被更新我添加以上至電網日期時間輸出。網格刷新日期時間不是。

@using GridMvc.Html 
@model Grid.Mvc.Ajax.GridExtensions.AjaxGrid<ReportInReviewViewModel> 
<p>@string.Format("{0}",DateTime.Now)</p> 
@Html.Grid(Model).Named("reportNumbersGridInReview").Columns(columns => 
         { 
          columns.Add(reportsinreview => reportsinreview.ReportNumber) 
          .Titled("Report number") 
          .Filterable(true) 
          .Sortable(true); 
        }).SetRowCssClasses(reportsinreview => "row_" + reportsinreview.ReportNumber).SetRowCssClasses(reportsinreview => (reportsinreview.IsAmber ? "amber" : string.Empty) + (reportsinreview.IsRed ? "red" : string.Empty)) 

如果我只能用一些htmlAttributes或其他方法來豐富網格,那麼我可以使用這些數據更新整個面板。這只是網格上方顯示的一些數量非常有限的聚合數據。

pageGrids.reportNumbersGridInReview.onGridLoaded(function (e) { 
    updatePanelData(); 
}); 

有沒有人曾經處理過這個問題?你是如何解決它的? 解決此問題的最佳做法是什麼?

在這一點上我很絕望。我正在考慮刪除grid.mvc.ajax包作爲一個整體,並回到我的第一次嘗試。然後每次面板重新加載時手動設置網格。

回答

0

這不是一個公認的答案,但有解決方法:

在控制器方法設置會議:

[HttpGet] 
    [AjaxOnly] 
    public ActionResult GetReportsInReviewGrid(int? page) 
    { 
     //Putting the totals model in Session is a hack, because grid.mvc.ajax will only upload the grid, not the panel. 
     var items = GetReportInReviewViewModels(); 
     Session[STATUSPANELTOTALSINREVIEW] = GetStatusPanelTotalsViewModel(items); //(items); 

     return GetReportsGrid(items, GRIDREPORTSINREVIEW_PARTIAL_PATH); 
    } 

然後onGridLoaded從會話檢索數據:

pageGrids.reportNumbersGridInReview.onGridLoaded(function (e) { 
    UpdateStatusPanelTotalsInReview(); 
}); 

相應的控制器方法:

[HttpGet] 
    [AjaxOnly] 
    public PartialViewResult GetStatusPanelTotalsInReview() 
    { 
     var model = Session[STATUSPANELTOTALSINREVIEW]; 
     return PartialView("Partials/_StatusPanelTotals", model); 
    } 

這是一種可行的解決方法。但當然,這是荒謬和醜陋的。 grid.mvc.ajax應該只是更新整個partialview而不僅僅是網格。

正如前面所討論的那樣:我可能只需要移除grid.mvc.ajax包並在每個面板更新上重新連接網格功能。兩種解決方案都很薄弱

+0

其實我應該提一下:[使用會話](https://www.simple-talk.com/cloud/platform-as-a-service/managing-session-state-in-windows-azure-what-are -the-options /)是一個糟糕的主意,特別是當考慮在雲中託管應用程序時,例如azure。 –