tags:

views:

31

answers:

2

Hello guys i am developing MVC application, I want to call a action (with 2 parameter) when i click on some button and on the event handler of the button i do the following :

            var url = '<%: Url.Action("SomeAction", "SomeConroller") %>'
            $(location).attr('href', url, new { param1 : value1 , param2: value2 });

but this not working .. how can i pass parameters in this case ??? any help ???

A: 

You need to manually create the URL, like this:

url + "?Param1=" + encodeURIComponent(value1) 
    + "&Param2=" + encodeURIComponent(value2)
SLaks
+1  A: 

You want to pass in your two parameters with the Url.Action method:

var url = '<%: Url.Action("SomeAction", "SomeConroller", new { param1 = value1, param2 = value2 }) %>'

Sorry, realised the parameter values may not be accessible in the method. You could add placeholders and replace them afterwards:

 var url = '<%: Url.Action("SomeAction", "SomeConroller", new { param1 = "VAL1", param2 = "VAL2" }) %>';
 url = url.replace("VAL1", encodeURIComponent(value1));
 url = url.replace("VAL2", encodeURIComponent(value2));
Andy Rose
You forgot to escape the values. However, that's a neat solution.
SLaks
@SLaks - Yes, I hit the post answer button and then realised that. I've added an alternative which, whilst a little clunky, still has the support of MVC routing creating the URL.
Andy Rose
@SLaks - Thanks for the heads up on encoding the parameter values, always best practice. I have updates the code accordingly.
Andy Rose
Light