首页 文章

如何从Core 2 RazorPage ViewModel处理程序返回PartialView

提问于
浏览
4

在Asp.Net MVC中,您可以通过执行以下操作轻松返回部分视图:

return PartialView("ModelName", Model);

这是如何在RazorPage ViewModel处理程序上完成的?

2 回答

  • 7

    我想通了 . 它并不像MVC那样直截了当 . 您必须创建一个空的 ViewDataDictionary() ,然后将其Model属性设置为partial的填充模型 .

    View Model / Handler

    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;
    }
    

    我现在可以通过ajax GET调用这个处理程序并让它返回部分's HTML. I can then set the partial' s 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);
        }
    });
    
  • 0

    非常感谢TechFisher搞清楚,这里有一个更清洁的例子 .

    public IActionResult OnGetTestPartial()
    {
        return new PartialViewResult()
        {
            ViewName = "Test",
            ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
            {
                Model = new TestPartialData { Data = "inputhere" },
            }
        };
    }
    

    文件名“Test.cshtml”中的部分视图与上述类位于同一文件夹中 .

    @using YourNamespace
    @model TestPartialData
    
    <div>Hello, model value: @Model.Data</div>
    

    用jquery加载它异步

    $("#someHtmlElementId").load("Your/Path/TestPartial");
    

相关问题