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"
Anastasios Selmani
  • 3,579
  • 3
  • 32
  • 48
  • 1
    Can we see the generated link after calling `params.exampleUrl(` ? – GGO Oct 17 '17 at 09:30
  • 4
    is this a typo? you have missed `replace` here: `params.exampleUrl('pr1Id', actualParameter1Id)` – adiga Oct 17 '17 at 09:30
  • What URL returned by `uri`? You're not providing `actualParameter1Id`, `actualParameter2Id` & `actualParameter3Id` contents yet respectively (see http://idownvotedbecau.se/beingunresponsive). – Tetsuya Yamamoto Oct 17 '17 at 09:46
  • I will edit my question adding the information missing and I respect the reason of the downvote but I really don't think 17 minutes of being unresponsive in a question that I have asked 32 minutes ago should be considered as being unresponsive to helpers. Some are in their jobs and if my senior asks me to see something else I will see that first. Thank you all again for their interest. – Anastasios Selmani Oct 17 '17 at 10:02
  • 1
    The issue is the `&` in your url (which should be just `&`) –  Oct 17 '17 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, .... });` –  Oct 17 '17 at 10:17
  • @StephenMuecke thanks for your answer. I think you should post it in order to accept it. – Anastasios Selmani Oct 17 '17 at 10:28

1 Answers1

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;