【问题标题】:How to return a PartialView from Core 2 RazorPage ViewModel Handler如何从 Core 2 Razor 页面 ViewModel 处理程序返回部分视图
【发布时间】:2018-08-17 00:59:37
【问题描述】:

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

return PartialView("ModelName", Model);

这是如何在 RazorPage ViewModel Handler 上完成的?

【问题讨论】:

    标签: asp.net-core-2.0 razor-pages


    【解决方案1】:

    我想通了。它不像在 MVC 中那样直接。您必须创建一个空的ViewDataDictionary(),然后将其 Model 属性设置为部分填充模型。

    查看模型/处理程序

    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 调用这个处理程序并让它返回部分的 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);
        }
    });
    

    【讨论】:

    • 你拯救了我的一天!非常感谢!
    • 太棒了,但我无法让我的 AJAX GET 工作,如果我返回新的 json,它工作正常...您的 Ajax 调用有什么想法或示例吗?非常感谢
    • 希望我在 2 天前找到这个。
    【解决方案2】:

    非常感谢 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");
    

    【讨论】:

      猜你喜欢
      • 2019-02-02
      • 2019-06-28
      • 2018-09-04
      • 1970-01-01
      • 2018-12-24
      • 2018-06-05
      • 2018-12-20
      • 1970-01-01
      • 2020-04-01
      相关资源
      最近更新 更多