6

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.

Affan Shahab
  • 838
  • 3
  • 17
  • 39

1 Answers1

21

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 }))";
Abbas Galiyakotwala
  • 2,949
  • 4
  • 19
  • 34
  • 1
    Thanks @Abbas galiyakot it really worked perfectly. But I have a question. Please let me know if there will be any problem using @Html.Raw() like cross scripting? spamming etc? – Affan Shahab Apr 13 '15 at 12:29
  • the risk is in how it is used If you are displaying user entered information it is better to use @Html.Encode(). In another words, if you are displaying non-user eneterd data you are safe to go with @Html.Raw() – Abbas Galiyakotwala Apr 13 '15 at 12:42
  • This worked for me --> window.location.href = "@Html.Raw(@Url.Action("ABC", "XYZ", new { A= ViewBag.A , B = ViewBag.B }))"; – sunnysidedown916 Apr 08 '16 at 18:07