Search code examples
javascriptasp.net-mvcurlurl-parametersurl-action

Passing url parameters from Url.Action variables in javascript


I would like to start my question by mentioning that I have read the questions

Passing dynamic javascript values using Url.action()

and

How to pass parameters from @Url.Action to controller

For having cleaner code and for having javascript functions that open modal windows with the url as an arguement I prefer not using Razor in my javascript files. For that reason I set all my Url actions inside the PageScripts of my .cshtml pages like this and I use the variables in my functions.

@section PageScripts {
    // render necessairy scripts
    <script type="text/javascript">
        view.params = {
            exampleUrl: "@Url.Action("ExampleAction", "ExampleController", new { parameter1Id = "pr1Id", parameter2Id = "pr2Id", parameter3Id = "pr3Id" })",
            // More url actions and page module parameters
        }
    };
    </script>
}

Such url actions are used on modal windows I have to open from selected rows where I have to use many parameters. The way I use them in my javascript functions is like this

var uri = params.exampleUrl.replace('pr1Id', actualParameter1Id).replace('pr2Id', actualParameter2Id).replace('pr3Id', actualParameter3Id);

My controller is like like this

public ActionResult ExampleAction(string parameter1Id, string parameter2Id, string parameter3Id){
    // do stuff and return View...
}

My problem is that when I pass one parameter my controller get's the value correctly. When I pass more than one parameters although I see from debugging the uri parameter has changed and has taken the new parameters, in my controller only the first one comes correctly. The other variables are coming null.

Examples:

addCandidateTaskUrl: "@Url.Action("Action", "Controller", new { candidacyId = "cId", candidateId = "cndId",  typeOfAction = "tpA" })"

Uri from params

"/ApplicationName/Controller/Action?candidacyId=cId&amp;candidateId=cnId&amp;typeOfAction=tpA"

Uri after replace of values

"/ApplicationName/Controller/Action?candidacyId=97c89ac6-3571-48a1-a904-d4b3f2594d9b&amp;candidateId=a05amn84-33a1-4fcb-8c89-e44635d67d63&amp;typeOfAction=COMPLETED"

Solution

  • You usage of Url.Action() in the script is generating &amp; instead of & in the url, so instead of

    ../Controller/Action?candidacyId=cId&amp;candidateId=cnId&amp;typeOfAction=tpA"
    

    you need to generate

    ../Controller/Action?candidacyId=cId&candidateId=cnId&typeOfAction=tpA"
    

    You can do this by wrapping it in Html.Raw() so its not encoded

    addCandidateTaskUrl: "@Html.Raw(Url.Action("Action", "Controller", new { candidacyId = "cId", candidateId = "cndId",  typeOfAction = "tpA" }))"
    

    However, you can make this simpler and more efficient (without the need for multiple .replace()) by just generating the base url, and the appending the query string parameters using the $.param method

    view.params = {
        baseUrl: '@Url.Action("Action", "Controller")' // Html.Raw not required
    

    and

    var queryString = $.param({ param1Id: actualParam1Id, param2Id: actualParam2Id, ... });
    var uri = params.baseUrl + '?' + queryString;