.net 删除后返回索引

hjzp0vay  于 2022-12-24  发布在  .NET
关注(0)|答案(1)|浏览(81)

删除项目后,我如何返回到索引页?删除后,我想关闭部分视图(模态)并返回到索引页

[BindProperty]
    public ItemDto Item{ get; set; } = new ItemDto();
    public PartialViewResult OnGetDelete(int id)
    {
        Invoice = _ItemApplication.GetDetails(id);
        return Partial("Delete", Item);
    }
    public JsonResult OnPostDelete(int id)
    {
        _ItemApplication.Delete(id);
         return new JsonResult(new { status = true, message = "Done!" });

    }
6yt4nkrj

6yt4nkrj1#

删除项目后,我如何返回到索引页?删除后,我想关闭部分视图(模态)并返回到索引页
好吧,因为你使用的是ajax,你必须在success中处理它,然后使用window.location.href重定向到你想要的页面。你可以参考下面的例子:

    • 解决方案:**
$.ajax({
                url: "/Controller/OnPostDelete",
                type: "DELETE",
                data: {
                    'id': 1
                },
                dataType: "json",
                success: function (response) {
                    console.log(response);
                    window.location.href = "@Url.Action("Index", "ControllerName")";
                    
                },
                error: function (xhr, error, thrown) {
                    alert("here2" + xhr.responseText);
                }
            });
    • 完整演示:**

删除请求Ajax:

$.ajax({
                url: "/Controller/OnPostDelete",
                type: "DELETE",
                data: {
                    'id': 1
                },
                dataType: "json",
                success: function (response) {
                    console.log(response);

                    window.location.href = "@Url.Action("Index", "Controller")?message=" + response.message;
                },
                error: function (xhr, error, thrown) {
                    alert("here2" + xhr.responseText);
                }
            });

注意:如果你想传递参数给你的new redirected view,那么你可以用这种方式来传递参数。如果不需要传递参数,那么"@Url.Action("Index", "ControllerName")";就可以了。
删除请求操作:

[HttpDelete] 
public JsonResult OnPostDelete(int id)
    {
        _ItemApplication.Delete(id);
         return new JsonResult(new { status = true, message = "Done!" });

    }

控制器需要重定向:

public IActionResult Index(string message)
        {
            ViewBag.message = message;
            return View();
        }

重定向时查看:

<h1>The Redirect Value is: @ViewBag.message</h1>

输出:

相关问题