如何在@ Url.Action中传递动态值?

时间:2023-01-23 15:12:21

I have written following jquery in my partial view:

我在部分视图中写了以下jquery:

    $.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.

操作名称和控制器名称不固定,它们必须根据放置此局部视图的视图而更改。我有获取调用操作和控制器名称的函数,但不知道如何在@ Url.Action中传递它们。

Following are Javascript functions to fetch action and controller names:

以下是用于获取操作和控制器名称的Javascript函数:

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;            
        }

3 个解决方案

#1


27  

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

我有获取调用操作和控制器名称的函数,但不知道如何在@ Url.Action中传递它们

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

好吧,你可以调用这些功能。例如,如果它们是UrlHelper类的扩展方法:

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)';

#2


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;

#3


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>"

#1


27  

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

我有获取调用操作和控制器名称的函数,但不知道如何在@ Url.Action中传递它们

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

好吧,你可以调用这些功能。例如,如果它们是UrlHelper类的扩展方法:

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)';

#2


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;

#3


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>"