Render partial view with webgrid control via ajax call
我在我的ajax调用上遇到问题,渲染包含web网格控件的局部视图。 我不能渲染部分视图。 我在这里做错了什么
我的观点我需要使用ajax调用替换响应文本
1 | @{if (Model.SearchResultSupplierViewModels.Count() > 0){ Html.RenderPartial("_SearchResult", Model.SearchResultSupplierViewModels);}} |
Ajax调用我的部分视图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $.ajax({ url: '@Url.Action("GetSearchResultBasedOnFilters","SearchResult")', contentType: 'text/html; charset=utf-8', type: 'GET', dataType: 'html', data: { supp_fullCheckList: supp_fullCheckList, practice_fullCheckList: practice_fullCheckList, county_fullCheckList: county_fullCheckList, state_fullCheckList: state_fullCheckList, ratebase_sliderValue: ratebase_sliderValue, clientRating_sliderValue: clientRating_sliderValue, panelRating_sliderValue: panelRating_sliderValue }, success: function (response) { $('#searchArea').html(response); alert(response); }, error: function (xhr, status, error) { alert(status +" :" + error); }}); |
部分视图与web网格控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @model List<Panelpartnership.Domain.SearchResultSupplierViewModel> @{ if (Model != null && Model.Count() > 0) { var grid = new WebGrid(source: Model, rowsPerPage: 10, ajaxUpdateContainerId:"searchArea", defaultSort:"SupplierId"); grid.GetHtml( tableStyle:"webgrid", alternatingRowStyle:"alt", columns: grid.Columns( grid.Column("SupplierId", header:"Supplier Id"), grid.Column("SupplierName", header:"Supplier Name"), grid.Column("SupplierLocation", header:"Supplier Location") ) ); } else { @:There are no suppliers. Please use different filtering conditions for more searching! } |
}
最后我的控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [HttpGet] public ActionResult GetSearchResultBasedOnFilters(string supp_fullCheckList, string practice_fullCheckList, string county_fullCheckList, string state_fullCheckList,string ratebase_sliderValue, string clientRating_sliderValue, string panelRating_sliderValue) { try { List<SearchResultSupplierViewModel> tSearchResultViewModel = new List<SearchResultSupplierViewModel>(); tSearchResultViewModel = searchresultmanager.GetSearchResultBasedOnFilterConditions(supp_fullCheckList, practice_fullCheckList, county_fullCheckList, state_fullCheckList, ratebase_sliderValue, clientRating_sliderValue, panelRating_sliderValue); return PartialView("_SearchResult", tSearchResultViewModel); } catch (Exception ex) { return Json(new { ok = false, message = ex.Message }); } } |
我想出了这个问题。 问题在于我的webgrid控件。 当我改变我的webgrid控件时,问题就消失了。 这是更新的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @model List<Panelpartnership.Domain.SearchResultSupplierViewModel> @{ if(Model != null && Model.Count()>0) { var grid = new WebGrid(Model, canPage: true, rowsPerPage: 5, selectionFieldName:"selectedRow", ajaxUpdateContainerId:"searchArea"); grid.Pager(WebGridPagerModes.NextPrevious); @grid.GetHtml(tableStyle:"webGrid", headerStyle:"header", alternatingRowStyle:"alt", selectedRowStyle:"select", columns: grid.Columns( grid.Column("SupplierId","Supplier Id"), grid.Column("SupplierName","Supplier Name"), grid.Column("SupplierLocation","Supplier Location") )) } else{ <label>No suppliers to display!</label> } } |