[英]Use the controller name from Javascript in the Url.Action
I am retrieving controller name from a javascript function as a string, and need to pass controller name in Url.Action. 我正在从JavaScript函数中以字符串的形式获取控制器名称,并且需要在Url.Action中传递控制器名称。 Any suggestion would be helpful. 任何建议都会有所帮助。
var controllerName = "MyController";
var id=1;
@Url.Action("LoadAction","'"+ controllerName +"'")?id=' + id
// Here i am unable to pass controllerName, defined using javascript.
Thanks. 谢谢。
If you want to send data from a js script to a C# controller, then you can use a Jquery-ajax call instead of @Url.Action
, if I'm not mistaken, you can't even use @Url.Action on a js source code. 如果要将数据从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.