javascript 在Javascript函数中获取MVC控制器名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20450401/
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-10-27 18:31:57  来源:igfitidea点击:

Get the MVC controller name in Javascript function

javascriptjqueryasp.net-mvc

提问by 10K35H 5H4KY4

I have a JqueryCRUD function; which is called from several Controllers actions. Is there any way to find out which contoller is tiggering that function.

我有一个JqueryCRUD 函数;这是从几个控制器操作调用的。有什么方法可以找出哪个控制器正在触发该功能。

For Example; function call from View:

例如; 从视图调用函数:

$('a.Edit-Icon').live("click", function (event) { 
  editDialog(this, event, '#_List'); 
});

Function parameters:

功能参数:

function editDialog(tag, event, target,value)
{
  ------
  // How to get the Controller name ???????????

}

Thanks in advance.....

提前致谢.....

回答by Sirwan Afifi

You can get controller's name in javascript this way using razor:

您可以使用 razor 以这种方式在 javascript 中获取控制器的名称:

var controllerName = '@ViewContext.RouteData.Values["Controller"].ToString()';

Or

或者

var controllerName='@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()';

Update:

更新

You can also get controller's name this way:

您还可以通过这种方式获取控制器的名称:

var controllerName = '@ViewContext.Controller.ValueProvider.GetValue("controller").RawValue';