隐藏浏览器地址栏中的网址

时间:2018-01-25 07:29:23

标签: javascript jquery asp.net-mvc razor

我正在使用.NET MVC 5来开发Web应用程序。我有一个带有单个参数的方法的控制器。

public class ProductController : Controller
{
    [HttpGet]
    public ActionResult Index(string ProductCode)
    {
        // Retrieve product details from database based on product code.
        ....
    }
}

在我的源Home.cshtml文件中,我有这个链接:

<a href="/Product/Index?ProductCode=123456">

当用户点击链接时,将调用ProductController,然后在产品视图中显示产品详细信息。

到目前为止一切正常。

我唯一的问题是,当用户点击该链接时,网址“http://..../Product?Index=ProductCode=123456”将显示在浏览器的地址栏中。如何阻止URL显示在地址栏中?

我猜我需要将ProductController.Index()中的[HttpGet]属性更改为[HttpPost]。然后在Home.cshtml文件中,如何修改它以进行HTTP POST调用而不是GET?我想我需要修改它来添加一个“onClick”来调用JavaScript或jQuery函数。

1 个答案:

答案 0 :(得分:0)

只需使用jquery并将参数作为数据属性添加到锚标记。

    $("a").click(function(){  
        window.location.href= $(this).attr("href") +"?ProductCode=" +$(this).data("productcode")
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<a href="/Product/Index" data-ProductCode="123456" >Click Me</a>