繁体   English   中英

在Url.Action中使用Javascript中的控制器名称

[英]Use the controller name from Javascript in the Url.Action

我正在从JavaScript函数中以字符串的形式获取控制器名称,并且需要在Url.Action中传递控制器名称。 任何建议都会有所帮助。

var controllerName = "MyController";
var id=1;
@Url.Action("LoadAction","'"+ controllerName +"'")?id=' + id
// Here i am unable to pass controllerName, defined using javascript.

谢谢。

如果要将数据从js脚本发送到C#控制器,则可以使用Jquery-ajax调用代替@Url.Action ,如果我没记错的话,甚至不能在@ Url.Action上使用js源代码。

const sendId = () => {
    const controllerName = 'MyController';
    const id = 1;
    $.ajax({
          contentType: 'application/json',
          data: { myId: id },
          url: `${controllerName}/methodName`, //template string
          type: 'POST',
          success: function (data) {
             //...
          },
          failed: function () {
            //...
          }
     });
  }

暂无
暂无

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

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