MVC剃刀不会在动作后重定向到url

本文关键字:重定向 url 剃刀 MVC | 更新日期: 2023-09-27 18:14:27

我想打开产品的编辑页面,但在索引操作之后,它不会从列表页面重定向到该页面。你可以在这里找到我的代码:

在我的列表页面:

    function getProductDetail(id) {
        $.ajax({
            type: "POST",
            url: '@Url.Action("Index","ProductDetail")',
            dataType: "html",
            data: JSON.stringify({ "productId": id }),
            contentType: "application/json",
            success: function (result) {
            }
        });
    }
</script>

在我的ProductDetailController上:

   public ActionResult Index(int productId)
        {
            Product prod = GetProductDetail(productId);
            return View(prod);
        }

MVC剃刀不会在动作后重定向到url

根据上面的评论,在这种情况下根本不需要使用AJAX。如果您计划使用对服务器的异步调用动态更新DOM,那么这是有意义的。在您的情况下,由于您只是重定向到页面,因此使用actionlink并完全摆脱AJAX调用将更有意义。

@HTML.ActionLink("Link Text","Index","ProductDetail",new {productId = "1234"}, null))