asp.net mvc - Passing url parameters from Url.Action variables in javascript - Stack Overflow

I would like to start my question by mentioning that I have read the questionsPassing dynamic javascrip

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 es correctly. The other variables are ing 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"

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 es correctly. The other variables are ing 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"
Share Improve this question edited Oct 17, 2017 at 10:06 Anastasios Selmani asked Oct 17, 2017 at 9:26 Anastasios SelmaniAnastasios Selmani 3,6893 gold badges35 silver badges49 bronze badges 7
  • 1 Can we see the generated link after calling params.exampleUrl( ? – GGO Commented Oct 17, 2017 at 9:30
  • 4 is this a typo? you have missed replace here: params.exampleUrl('pr1Id', actualParameter1Id) – adiga 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). – Tetsuya Yamamoto Commented Oct 17, 2017 at 9:46
  • 1 The issue is the &amp; in your url (which should be just &) – user3559349 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, .... }); – user3559349 Commented Oct 17, 2017 at 10:17
 |  Show 2 more ments

1 Answer 1

Reset to default 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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745482289a4629612.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信