1

I'm trying to pass Username and Password using @Url.Action method but it's only passing one parameter and the second one is null. I've tried all alternatives with no success. I've also tried to pass an object of the class type with no success. This is a short sample code:

string User = ViewBag.Username;
string Pass = ViewBag.Pass;

 case "CHANGE_NEXT_LOGON":
                        <script type="text/javascript">
                            BootstrapDialog.show({
                                title: 'Change Password',
                                message: $('<div></div>').load('@Url.Action("PasswordChange", "UserPassword", new { Username = User, Password = Pass})'),
                                type: BootstrapDialog.TYPE_PRIMARY
                            });
                        </script>
                            break;

I'm using a boostrap dialog plugin to generate modal popups for validation. But unlike the RedirectToAction Method, the @Url.Action method doesn't seem to work the same way. Can someone give me a hand?

thanks!

Christos
  • 53,228
  • 8
  • 76
  • 108
Alexander P.
  • 382
  • 1
  • 7
  • 19
  • 1
    Have you defined a Route for the additional parameter? Default route accepts only one (optional id). –  Aug 12 '15 at 14:16
  • Try to assign @URL.Action to a (var) in your script and then debug the script to make sure of the generated URL – Ala Aug 12 '15 at 14:35
  • @Darren I have not, I believe you refer to create a Route constraint or something? – Alexander P. Aug 12 '15 at 14:37
  • @Ala The page redirects just fine, and it even send one parameter but not the second one, that's the problem. I don't think is a matter of syntax but somehow an issue in the parameters passed. – Alexander P. Aug 12 '15 at 14:39
  • When you debug your script, did you make sure that both parameters have values? – Ala Aug 12 '15 at 14:51
  • You can pass as many values as your want (so long as you don't exceed the query string limit). Show your controller method for `PasswordChange()` –  Aug 13 '15 at 00:54
  • I did some work around here. Since it was just accepting one parameter, and nothing I tried worked at all, I just concatenate the two string into one comma separated variable, then send it over, split it in an array and just used both positions. I know is kind of a bad way, but it works as a charm. – Alexander P. Aug 13 '15 at 13:36
  • Have a quick look at first answer to this Q http://stackoverflow.com/questions/2246481/routing-with-multiple-parameters-using-asp-net-mvc –  Aug 13 '15 at 14:24

3 Answers3

4

I found the solution for this. The problem is that since you are passing @Url.Action within the Jquery.Load() method it is having a problem with the returned url. The @Url.Action return the & encoded in the url so to resolve this you must encapsulate the whole method in @Html.Raw(@Url.Action) and it will work.

Alexander P.
  • 382
  • 1
  • 7
  • 19
2

This

'@Url.Action("PasswordChange", "UserPassword", new { Username, User, Password = Pass})'

should be like this

'@Url.Action("PasswordChange", "UserPassword", new { Username = User, Password = Pass})'

You have a mistype in the object initializer.

Christos
  • 53,228
  • 8
  • 76
  • 108
0

In my case, i could pass up to 5 parameters to Controllers via post/get methods with input, button, or a elements using razor syntax (.cshtml) in View. But for unknown reason, since yesterday, although i used the same syntax, i failed to pass the third parameter of an input element. Its third parameter value was changed to "formaction" automatically.