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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16052704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:17:08  来源:igfitidea点击:

How to pass dynamic value in @Url.Action?

jqueryasp.net-mvc-4url.action

提问by Nirman

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.

Action 名称和 Controller 名称不是固定的,它们必然会根据放置此局部视图的视图而变化。我有获取调用动作和控制器名称的函数,但不确定如何在@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;            
        }

回答by Darin Dimitrov

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

回答by mridula

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;

回答by Mario Villanueva

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