【问题标题】:return Page() in ASP.NET Core Razor Pages doesn't render the page after calling by RedirectToPageASP.NET Core Razor Pages 中的 return Page() 在 RedirectToPage 调用后不呈现页面
【发布时间】:2021-01-09 08:50:28
【问题描述】:

我是 ASP.NET Core Razor 页面的新手。

在索引页面中,我获取客户数据并通过来自 CustomerController 的 ajax 在 jQuery 数据表中显示它们。最后一列有删除按钮。通过这些按钮,我在 CustomerController 中调用 delete 方法。

JS Ajax 文件

$(document).ready(function() {
$("#customerDataTable").dataTable({
    "processing": true,
    "serverSide": true,
    "filter": true,
    "ajax": {
        "url": "/api/customer",
        "type": "post",
        "datatype": "json"
    },
    "columnDefs":[
        {
            "targets": [0],
            "visible": false,
            "searchable": false
        }],
    "columns":[
        { "data": "id", "name": "Id", "autoWidth": true },
        { "data": "firstName", "name": "First Name", "autoWidth": true },
        { "data": "lastName", "name": "Last Name", "autoWidth": true },
        { "data": "contact", "name": "Contact", "autoWidth": true },
        { "data": "email", "name": "Email", "autoWidth": true },
        { "data": "dateOfBirth", "name": "Date Of Birth", "autoWidth": true },
        {
            "render": function(data,type, row) {
                return "<a href='#' class='btn btn-danger' onclick=deleteCustomer('" + row.id + "'); 
        >Delete</a>"; }
                 }]
        });
    });

  function deleteCustomer(id) {
         $.ajax({
            url: "/api/customer",
            type: "GET",
            data: id
         });
    }

在 CustomerContoller 中,我想使用参数重定向到 DeleteCustomer 剃须刀页面。

    [HttpGet]
    public IActionResult DeleteCustomer()
    {
        var id = Request.Query.Keys.ToList()[0];
        return RedirectToPage("/DeleteCustomer", new{id});
    }

在 DeleteCustomer 页面中有一个简单的 get 方法。

    [BindProperty]
    public int CustomerId { get; set; }

    public IActionResult OnGet(string id)
    {
        CustomerId = int.Parse(id);
        return Page();
    }

当我逐行调试应用程序时,CustomerControll 正确重定向到 DeleteCustomer 页面,并且页面中的 OnGet 方法返回 Page() 方法,但浏览器不会呈现页面的 HTML 部分而没有任何错误。只是索引页面仍然显示数据表。

如果我在地址栏中调用 DeleteCustomer 页面,DeleteCustomer 页面将呈现。

为什么return Page()在浏览器中被RedirectToPage调用后不渲染显示页面?

【问题讨论】:

    标签: asp.net-core razor-pages


    【解决方案1】:

    为什么在浏览器中被RedirectToPage调用后return Page()不渲染显示页面?

    您从客户端发出 Ajax 请求,它不会自动重新加载/刷新页面。要显示返回的页面内容,可以尝试在成功回调函数中动态编写,如下所示。

    $.ajax({
        url: "/api/customer",
        type: "GET",
        data: id,
        success: function (res) {
            //console.log(res);
            document.write(res);
        }
    });
    

    此外,您还可以尝试修改方法以使您的 API 操作 DeleteCustomer 返回一个 id,然后在客户端进行重定向,如下所示。

    [HttpGet]
    public IActionResult DeleteCustomer()
    {
        //your code logic here
    
        //var id = Request.Query["id"];
        //return RedirectToPage("/DeleteCustomer", new { id });
    
        return Ok(new { id });
    } 
    

    在客户端

    success: function (res) {
        console.log(res.id[0]);
    
        window.location.assign("/DeleteCustomer?id=" + res.id[0]);
        //document.write(res);
    }
    

    【讨论】:

    • 谢谢。使用 document.write() 结果将显示在当前页面中,表示索引。如何重定向到另一个页面?
    • How can I redirect to another page?你好@Parham.D,请参考我帖子中的第二种方法,并在成功回调函数中在客户端进行重定向。
    • Is it possible to call a razor page without Ajax in JavaScript? 可以生成并在表单中放置删除按钮,并将表单提交给特定的页面处理程序。
    • 谢谢。而且我也可以使用 document.location.assign("/DeleteCustomer?id=" + id);直接。
    • And also I can use document.location.assign("/DeleteCustomer?id=" + id); directly. 是的:-)
    猜你喜欢
    • 2019-08-15
    • 2022-01-20
    • 2021-07-17
    • 2018-04-05
    • 1970-01-01
    • 2019-04-10
    • 2021-01-17
    • 1970-01-01
    • 2022-07-14
    相关资源
    最近更新 更多