2

I want to give dynamic action name in Url.action through javascript.

// I want to change Index name by dynamic
      $.ajax({
                        url: '@Url.Action("Index", "Home")', 
                        type: "Post",
                        data: { Surveyid: surveyid, Category: catcode },
                        success: function (data) {
                            window.location.href = data.Url
                        }

like

var x="xxxx";

@Url.Action(x,"Home") -> not working throws error

@Url.Action(x.toString(),"Home") -> not working

then how can i ?

2
  • Is "x" client variable or server variable? If "x" is client variable, you cant use it in server code (in Url.Action) Commented Jan 2, 2014 at 14:11
  • no everything is clientside only.i meant x and url.action everything in clientside Commented Jan 2, 2014 at 14:13

3 Answers 3

3

Url.Action is server generated, whereas it seems you want to change the action on the browser. What you can do is tokenize the Action, get Url.Action to generate the tokenized URL, and then substitute this in js:

var jsUrl = '@Url.Action("##", "Home")'; // ## is the token
$.ajax({
    url: jsUrl.replace('##', someDynamicAction),
    ... 

(You may need to do the same for the controller)

Edit

My conscience has gotten the better of me - doing this isn't a good idea, given that any invalid action name (or a change in the Controller or Action names) will only be picked up at run time e.g. with 404 errors.

The number of controllers and actions that you need to ajax to should be finite, and T4MVC has already solved this kind of issue.

You can create the urls to the various links:

var urlToIndex = '@Url.Action(MyControllerAssembly.Index.Home())))';
var urlToOtherAction = ...
... etc for all actions needed in the 'switch' for ajax call.

and then choose the appropriate URL for your ajax call. (T4MVC also has methods Url.JavaScriptReplacableUrl and Ajax.ActionLink although for slightly different scenarios)

Sign up to request clarification or add additional context in comments.

Comments

3

Best way to use urls in your mvc application is that you define a global app_url in layout page like following:

_Layout.cshtml

<script>
    var app_root = '@Url.Content("~/")';
</script>

and use in content page

any_page that inherited from _Layout.cshtml

 $.ajax({
     url: app_root + 'Home/Index', // or url: app_root + 'Home/' + x

Comments

0

something odd in your example, but I believe what you are trying to do is this

function axajThis(numberthing,path) {

    var newUrl = "htttp://somplace/"+path; 
     /* and what ever the number x is doing */
      $.ajax({
         url: newUrl, 
         type: "Post",
         data:  .... 
       });
    };
 }

 axajThis(x,"Home");

Little more info needed on what 'x' is.

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.