How to pass multiple parameters in Url.Action()

Affan Shahab picture Affan Shahab · Apr 13, 2015 · Viewed 24.1k times · Source

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

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

And this is my Method in Controller 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 B first. 2nd one is always null. VIEW is basically under JavaScript function. Here is the URL: http://localhost/CargoMainSite/XYZ/ABC?A=1&B=2 Please 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.

Answer

Abbas Galiyakotwala picture Abbas Galiyakotwala · Apr 13, 2015

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

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