首页 > 解决方案 > 如何从asp.net核心MVC jquery中的链接隐藏Id参数?

问题描述

这是我重定向到我的视图的脚本

<script>
    $(document).on('click', '.btndetail', function () {
            var id = $(this).data("id");
            var url = "/Schools/SchoolDetails/Id=" + id;
            window.location.href = url;
        }
    );
</script>

和 id 进入控制器

 [HttpGet]
 public IActionResult SchoolDetails(string Id)
 {
     return View();
 }

http://localhost:55696/Schools/SchoolDetails/Id=1234

但是 ID 显示在链接中,我该如何隐藏它?

标签: c#jquerymodel-view-controller

解决方案


您需要实现 ajax 方法,使用 GET 进行异步调用,请参阅此文档: http ://api.jquery.com/jquery.ajax/

这是在您的代码中实现的示例:

$(document).on('click', '.btndetail', function () {
        var id = $(this).data("id");
        var url = '@Url.Action("SchoolDetails", "Schools")';
        $.ajax({
            type: "GET",
            url: url,
            data: { Id = id },
            success: function (data) {
                // Success Here
                window.location.href = url;

            },
            error: function (data) {
                alert("Error!");
            }
        })
    })

推荐阅读