[英]mvc grid ajax , grid is updated but the partialview is not
在C#Web應用程序上進行維護時,我遇到了MVC Grid 。 我的一個視圖有4個面板,每個面板都有一個包含大量數據的網格。 出於性能原因,我希望能夠使用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);
}
為了直觀地證明部分內容未更新,我在網格上方添加了datetime輸出。 日期時間未刷新網格。
@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包,然后再回到我的第一次嘗試。 然后,每次重新加載面板時,只需手動設置Grid。
這不是可接受的答案,而是一種解決方法:
在控制器方法中,設置會話:
[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您從Session中檢索該數據:
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包,並在每次面板更新時重新連接網格功能。 兩種解決方案都很弱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.