Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass dynamic value in @Url.Action?

Tags:

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;                     } 
like image 733
Nirman Avatar asked Apr 17 '13 06:04

Nirman


People also ask

How to pass dynamic values in Url in JavaScript?

You can concat the client-side variables with the server-side url generated by this method, which is a string on the output. Try something like this: var firstname = "abc"; var username = "abcd"; location. href = '@Url.


1 Answers

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)'; 
like image 184
Darin Dimitrov Avatar answered Oct 01 '22 14:10

Darin Dimitrov