简体   繁体   English

如何将动作和控制器传递给@Url.action 调用

[英]How to pass the action and controller to the @Url.action call

I would like to pass the action name and controller name to the @Url.action call within a Javascript function as variables.我想将动作名称和控制器名称作为变量传递给 Javascript 函数中的 @Url.action 调用。 Is there a method to achieve this?有没有办法做到这一点?

function list_onSelectionChanged(e) {
    var strActionName = "Map";
    var strControllerName = "PMO";
    $("#content").load("@Url.Action(strActionName,strControllerName)");
}

You can't pass them from client-side code in this way because the server-side code has already run at that point.您不能以这种方式从客户端代码传递它们,因为此时服务器端代码已经运行。 Specify them directly in the server-side operation:在服务器端操作中直接指定它们:

function list_onSelectionChanged(e) {
  $("#content").load("@Url.Action("Map", "PMO")");
}

Note that it looks like the syntax will fail because of nested double-quotes.请注意,由于嵌套的双引号,语法似乎会失败。 However, keep in mind that the Razor parser identifies the server-side code separately from the client-side code.但是,请记住,Razor 解析器将服务器端代码与客户端代码分开识别。 So this is the entire server-side operation:所以这是整个服务器端操作:

Url.Action("Map", "PMO")

And the result of that operation is emitted to the client-side code:该操作的结果被发送到客户端代码:

$("#content").load("result_of_server_side_operation");

I was able to modify the script as follows:我能够按如下方式修改脚本:

function list_onSelectionChanged(e) {
    var url = e.addedItems[0].path;
    $.get(url, function (data) {
        $("#content").html(data);
    });
}

Even simpler:更简单:

$("#content").load(url); $("#content").load(url);

This allows a variable to be passed for the url.这允许为 url 传递变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM