Tec*_*her 7 asp.net-core-2.0 razor-pages
在Asp.Net MVC中,您可以通过执行以下操作轻松返回部分视图:
return PartialView("ModelName", Model);
这是如何在RazorPage ViewModel处理程序上完成的?
我想通了.它并不像MVC那样直截了当.您必须创建一个空ViewDataDictionary(),然后将其Model属性设置为partial的填充模型.
查看模型/处理程序
public async Task<IActionResult> OnGetAsyncUpdateSearchResults(DateTime startDate, DateTime endDate, string selectedTypes)
{
int[] types = selectedTypes.Split(",").Select(x => int.Parse(x)).ToArray();
var inventory = await _itemService.GetFiltered(types, null, null, null, null, null, null, startDate, endDate.ToUniversalTime(), null, null, null, null, null, null, null);
if (inventory != null)
{
SearchResultsGridPartialModel = new SearchResultsGridPartialModel();
SearchResultsGridPartialModel.TotalCount = inventory.TotalCount;
SearchResultsGridPartialModel.TotalPages = inventory.TotalPages;
SearchResultsGridPartialModel.PageNumber = inventory.PageNumber;
SearchResultsGridPartialModel.Items = inventory.Items;
}
var myViewData = new ViewDataDictionary(new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary()) { { "SearchResultsGridPartialModel", SearchResultsGridPartialModel } };
myViewData.Model = SearchResultsGridPartialModel;
PartialViewResult result = new PartialViewResult()
{
ViewName = "SearchResultsGridPartial",
ViewData = myViewData,
};
return result;
}
Run Code Online (Sandbox Code Playgroud)
我现在可以通过ajax GET调用这个处理程序并让它返回部分的HTML.然后我可以div按预期设置部分刷新和部分刷新.
这是我正在制作的AJAX调用:
var jsonData = { "startDate": startDate, "endDate": endDate, "selectedTypes": selectedTypesAsString };
$.ajax({
type: 'GET',
url: "searchresults/?handler=AsyncUpdateSearchResults",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN", $('input:hidden[name="__RequestVerificationToken"]').val());
},
contentType: 'application/json; charset=utf-8"',
data: jsonData,
success: function (result) {
$("#searchResultsGrid").html(result);
},
error: function (error) {
console.log(error);
}
});
Run Code Online (Sandbox Code Playgroud)
Thanks alot to TechFisher for figuring it out, here is a bit cleaner example.
public IActionResult OnGetTestPartial()
{
return new PartialViewResult()
{
ViewName = "Test",
ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = new TestPartialData { Data = "inputhere" },
}
};
}
Run Code Online (Sandbox Code Playgroud)
Partial view in a file name "Test.cshtml" in the same folder as the above class.
@using YourNamespace
@model TestPartialData
<div>Hello, model value: @Model.Data</div>
Run Code Online (Sandbox Code Playgroud)
Load it async with jquery
$("#someHtmlElementId").load("Your/Path/TestPartial");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5421 次 |
| 最近记录: |