我想用一个调用刷新部分视图(主视图),以减少网络上的流量。一个分部视图里面有多个分部视图(子视图)。但是,我不能使用标准的mainpartialview.html(response)方法,因为其中一个子部分视图与脚本文件有冲突。我想继续使用JSON然而,模型中的languageForm和translationForm的值是空的。我发现的建议是要求我使用microsoft.aspnetcore.mvc.viewfeatures然而,它不是Net 6.
老方法
retun PartialView("~/Views/Facility/Translation/_PanelMain.cshtml", result);
新方法
return Json(new
{
languageForm = PartialView("~/Views/Facility/Translation/_Form_Language.cshtml", result.record),
translationForm = PartialView("~/Views/Facility/Translation/_PanelSub.cshtml", result.subRecord),
translationViewModel = result.subRecord
});
从接受的答案,下面是完整的解决方案
c#控制器
[HttpGet]
public async Task<ActionResult> GetLanguageRecord(long? languageID)
{
LoadViewBag();
var loadOptions = GetDefaultDataSourceLoadOptions();
var languageList = _languageService.GetGridViewList(GetMembershipID(), null, loadOptions);
var translationList = _translationService.GetGridViewList(GetMembershipID(), languageID, loadOptions);
var result = PopulateViewModel(languageID ?? 0, 0, languageList, translationList, loadOptions);
result.record.TransactionTypeID = SubmissionType.Edit;
var services = HttpContext.RequestServices;
var options = services.GetRequiredService<IOptions<MvcViewOptions>>().Value;
var executor = (PartialViewResultExecutor)services.GetRequiredService<IActionResultExecutor<PartialViewResult>>();
return Json(new
{
languageForm = await Render(
options,
ControllerContext,
PartialView("~/Views/Facility/Translation/_Form_Language.cshtml", result.record),
executor),
translationForm = await Render(
options,
ControllerContext,
PartialView("~/Views/Facility/Translation/_PanelSub.cshtml", result.subRecord),
executor),
translationViewModel = result.subRecord
});
}
Javascript
function grdMain_Language_selection_changed(selectedItems) {
var data = selectedItems.selectedRowsData[0];
if (data) {
$.ajax({
type: "GET",
url: "Translation/GetLanguageRecord",
data: { languageID: data.ID },
cache: false,
success: function (response) {
document.getElementById("_EntryForm_Language").innerHTML = response.languageForm;
document.getElementById("_SubPanel").innerHTML = response.translationForm;
$('#_subPanel_body').collapse('hide');
$('#_mainPanel_body').collapse('show');
myData_Translation = response.translationViewModel.recordList;
myTotalCount_Translation = response.translationViewModel.recordTotal;
let gvElement_Translation = document.getElementById("grdSub_Translation");
let gvInstance_Translation = DevExpress.ui.dxDataGrid.getInstance(gvElement_Translation);
gvInstance_Translation.refresh();
}
});
$(window).scrollTop(0);
}
}
在快速浏览了return PartialView("viewname");
的内部实现之后。我认为这是你需要将部分视图渲染成字符串的最小值;
public async Task<string> Render(MvcViewOptions options, ActionContext context, PartialViewResult result, PartialViewResultExecutor executor)
{
var viewEngineResult = executor.FindView(context, result);
viewEngineResult.EnsureSuccessful(originalLocations: null);
var view = viewEngineResult.View;
using (view as IDisposable)
{
using var writer = new StringWriter();
var viewContext = new ViewContext(
context,
view,
result.ViewData,
result.TempData,
writer,
options.HtmlHelperOptions);
await view.RenderAsync(viewContext);
await writer.FlushAsync();
return writer.ToString();
}
}
你会用像;
var services = HttpContext.RequestServices;
var options = services.GetRequiredService<IOptions<MvcViewOptions>>().Value;
var executor = (PartialViewResultExecutor)services.GetRequiredService<IActionResultExecutor<PartialViewResult>>();
return Json(new {
languageForm = await Render(
options,
ControllerContext,
PartialView("~/Views/Facility/Translation/_Form_Language.cshtml", result.record),
executor),
...
});
只要多花点功夫,你就可以使用Utf8JsonWriter
来将每个视图html直接呈现给响应体,而不需要任何缓存。