22

I have written following jquery in my partial view:

    $.ajax({
        type: "POST",
        url: '@Url.Action("PostActionName", "ControllerName")',
        data: { Id: "01" },
        success: function(data)
            {
            if (data.success="true")
                {
                    window.location = '@Url.Action("GetActionName", "ControllerName")'
                }
            }
    });

The Action name and Controller name are not fixed, they are bound to change depending upon the view wherein this partial view is placed. I have functions to fetch invoking action and controller names, but not sure how I can pass them in @Url.Action.

Following are Javascript functions to fetch action and controller names:

function ControllerName() {
            var pathComponents = window.location.pathname.split('/');
            var controllerName;
            if (pathComponents.length >= 2) {
                if (pathComponents[0] != '') {
                    controllerName = pathComponents[0];
                }
                else {
                    controllerName = pathComponents[1];
                }
            }
            return controllerName;
        }

        function ActionName() {
            var pathComponents = window.location.pathname.split('/');
            var actionName;
            if (pathComponents.length >= 2) {
                if (pathComponents[0] != '') {
                    actionName = pathComponents[1];
                }
                else {
                    actionName = pathComponents[2];
                }
            }
            return actionName;            
        }
Nirman
  • 6,715
  • 19
  • 72
  • 139

3 Answers3

33

I have functions to fetch invoking action and controller names, but not sure how I can pass them in @Url.Action

Well, you could call those functions. For example if they are extension methods to the UrlHelper class:

window.location = '@Url.Action(Url.MyFunction1(), Url.MyFunction2())'

or if they are just static functions:

window.location = '@Url.Action(SomeClass.MyFunction1(), SomeClass.MyFunction2())'

If on the other hand the values that need to be passed are known only on the client you could do the following:

var param = 'some dynamic value known on the client';
var url = '@Url.Action("SomeAction", "SomeController", new { someParam = "__param__" })';
window.location.href = url.replace('__param__', encodeURIComponent(param));

UPDATE:

It seems that you are just trying to fetch the current controller and action which could be achieved like that:

@{
    string currentAction = Html.ViewContext.RouteData.GetRequiredString("action");
    string currentController = Html.ViewContext.RouteData.GetRequiredString("controller");
}

and then:

window.location.href = '@Url.Action(currentAction, currentController)';
Donnelle
  • 5,689
  • 3
  • 27
  • 31
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I forgot to mention, they are javascript functions.. see the edit to see those functions. – Nirman Apr 17 '13 at 06:11
  • thanks Darin for the detailed answer, however, I am not sure what you meant by "SomeAction" and "SomeController". Should this be a name of target Action? or should it be dynamic? – Nirman Apr 17 '13 at 06:16
  • 1
    You seem to be attempting to fetch the current action and controller. See my updated example and do not use javascript for that. – Darin Dimitrov Apr 17 '13 at 06:18
  • @DarinDimitrov - As you have suggested, var param = 'some dynamic value known on the client'; var url = '@Url.Action("SomeAction", "SomeController", new { someParam = "__param__" })'; window.location.href = url.replace('__param__', encodeUriComponent(param));. Here why should not we pass the variable param straight away to the Url.action()? Can you please explain? – Manikandan Jun 09 '15 at 03:47
  • This is working well . Can't we just do this for multiple parameters in the same way ? We can probably use replace method few times using some temp variables, but just curious whether there are better ways – Peck_conyon May 13 '17 at 02:05
4

Have you tried something like this? I haven't tried it myself, but it should work.

var dataToSend = "01";
var url = '/controllerName/actionName/' + dataToSend;
var actionName = ActionName();
var controllerName = ControllerName();
url.replace("actionName",actionName);
url.replace("controllerName",controllerName);
window.location = url;
mridula
  • 3,203
  • 3
  • 32
  • 55
1
   function functionName(var1, var2) {var link = '@Url.Action("MethodName", "Controller", new { id = "-1", name = "-2" })';
    link = link.replace("-1", var1);
    link = link.replace("-2", var2);}

before, replace:

 html += "<a class='k-button k-button-icontext k-primary' href='" + link + "'><i class='fa fa-download'></i> Name Button</a>"
Mario Villanueva
  • 327
  • 3
  • 10