Javascript 如何在 Url.Action() 中传递多个参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29604626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:38:57  来源:igfitidea点击:

How to pass multiple parameters in Url.Action()

javascriptasp.net-mvcparametersactionresult

提问by Affan Shahab

I want to pass multiple parameters from Url.Action, Here is code in view

我想从 Url.Action 传递多个参数,这是视图中的代码

 window.location.href = "@Url.Action("ABC", "XYZ", new { @A= ViewBag.A , @B =  ViewBag.B })";

And this is my Method in Controller XYZ

这是我在控制器XYZ 中的方法

 public ActionResult ABC(string A, string B)
 {
    // Some Code
 }

I always get values in first parameter only and 2nd one is always null. Either if I but Bfirst. 2nd one is always null. VIEW is basically under JavaScript function. Here is the URL: http://localhost/CargoMainSite/XYZ/ABC?A=1&B=2Please note there is some extra text between Parameter one and Parameter two, that is "amp;" if I explicitly removes it. It works fine and get proper values.

我总是只在第一个参数中获取值,而第二个参数始终为空。如果我首先是B。第二个始终为空。VIEW 基本上是在 JavaScript 下的功能。这是网址:http://localhost/CargoMainSite/XYZ/ABC?A=1&B=2请注意参数一和参数二之间有一些额外的文字,即“amp;” 如果我明确删除它。它工作正常并获得正确的值。

回答by Abbas Galiyakotwala

Reason why Url.Action not working is that the & char in url is encoded, so you must use @Html.Rawas below

Url.Action 不工作的原因是 url 中的 & 字符被编码,所以你必须使用@Html.Raw如下

 window.location.href = "@Html.Raw(@Url.Action("ABC", "XYZ", new { @A= ViewBag.A , @B =  ViewBag.B }))";