3

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"
7
  • 1
    Can we see the generated link after calling params.exampleUrl( ? Commented Oct 17, 2017 at 9:30
  • 4
    is this a typo? you have missed replace here: params.exampleUrl('pr1Id', actualParameter1Id) Commented Oct 17, 2017 at 9:30
  • What URL returned by uri? You're not providing actualParameter1Id, actualParameter2Id & actualParameter3Id contents yet respectively (see idownvotedbecau.se/beingunresponsive). Commented Oct 17, 2017 at 9:46
  • 1
    The issue is the &amp; in your url (which should be just &) Commented Oct 17, 2017 at 10:11
  • 1
    You should be able to use @Html.Raw(Url.Action(...) to generate the correct url, but this is an inefficient solution (using multiple .replace). Just create the base url using exampleUrl: "@Url.Action("ExampleAction", "ExampleController"); and then append the query string values e.g. using var uri = params.exampleUrl + '?' + $.param({ parameter1Id: actualParameter1Id, parameter2Id: actualParameter2Id, .... }); Commented Oct 17, 2017 at 10:17

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.