【问题标题】:Asp.net Core 2 Razor pages: How display ajax partial page return?Asp.net Core 2 Razor 页面:如何显示 ajax 部分页面返回?
【发布时间】:2018-09-04 19:06:35
【问题描述】:

我的第一个项目是使用 Core 2.0 Razor 页面开始的,我之前对 MVC 5 有一点经验(以前的项目运行良好)。

Razor 页面和 AJAX 的问题很简单:

  • 有一个包含客户列表的页面

  • 使用 AJAX 编辑客户的链接以调用 Razor 部分页面 Customer_editPartial(它将完整的客户表单返回到项目中的模态 DIV)

问题是如何渲染Ajax调用返回的部分页面?如果我从 customer_editPartial.cs 返回一个带有文本的新 json,它正在工作,但无法显示部分页面。

感谢您的帮助。

这里是页面customers.cshtml

<script>
function editCustomer(id) {
    $.ajax({
        type: "GET",
        url: "Customer_editPartial?id=0",
        contentType: "application/json",
        beforeSend: function (xhr) {
            xhr.setRequestHeader("XSRF-TOKEN",
                $('input:hidden[name="__RequestVerificationToken"]').val());
        },
        dataType: "json",
        success: function (response) {
            AjaxCustomer_LoadSuccess(response);
        },
        failure: function (response) {
            alert('Customer content load failed.');
        }

    })
}

function AjaxCustomer_LoadSuccess(data) {
    $('#displayContent').html(data);
}
</script>
<a onclick="editCustomer(0);">Edit customer</a>
Here the ajax return<br />

页面 Customer_editPartial.cshtml

@page
@model UGM_Web.Pages.AHadmin.Customer_editPartialModel
edit partial form

这里是 Customer_editPartial.cs

       [ValidateAntiForgeryToken]
    public IActionResult OnGet(int? id)
    {
        return Page();
        //return new JsonResult("This is working when i return json text");

    }

    public void OnPost()
    {
    }

【问题讨论】:

  • 有人可以帮帮我吗?

标签: c# asp.net-mvc asp.net-ajax razor-pages


【解决方案1】:

主要答案是通过 TechFisher 在另一篇文章 (How to return a PartialView from Core 2 RazorPage ViewModel Handler) 中找到的。

但同时在我的代码中,我遇到了 ajax 数据类型错误(它被设置为 json 而不是 html :( 这当然不能用于接收部分视图......)。 同样在部分页面“Customer_editPartial.cshtml”中,您需要删除“@page”指令。

现在在您的 razor 页面中返回一个部分页面,您只需要设置一个 PartialViewresult 对象并返回它:

     public IActionResult OnGet(int? id)
    {
        Customer_editPartialModel _model = new Customer_editPartialModel(_dbContext);
        var myViewData = new ViewDataDictionary(new Microsoft.AspNetCore.Mvc.ModelBinding.EmptyModelMetadataProvider(),
                                new Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary());
        PartialViewResult _resultPartialPage = new PartialViewResult()
        {
            ViewName = "Customer_editPartial",
            ViewData = myViewData,
        };
        return _resultPartialPage;
    }

编辑:更简单的解决方案

return new PartialViewResult() { ViewName = "Customer_editPartial", ViewData = this.ViewData };

【讨论】:

  • 如果我在“public IActionResult”之前设置 [ValidateAntiForgeryToken] 它将不起作用。没有它 - 工作。也许你知道为什么?
  • Ella,要使用防伪令牌,您还需要在您的 cshtml 中添加防伪密钥:@Html.AntiForgeryToken()
  • 我发现了我的问题:需要在startup.cs中添加标头令牌名称:services.AddAntiforgery(options => options.HeaderName = "XSRF-TOKEN");
  • 为什么不直接使用内置控制器函数 PartialView(string viewName, object Model)?它返回一个 PartialViewResult 并相应地绑定模型中的数据。
猜你喜欢
  • 2019-06-28
  • 2018-03-06
  • 2020-02-09
  • 2018-04-05
  • 2019-03-12
  • 2021-06-26
  • 2019-08-04
  • 2018-08-17
  • 2018-12-20
相关资源
最近更新 更多