I have a Jquery CRUD function; which is called from several Controllers actions. Is there any way to find out which contoller is tiggering that function.
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.....
In ASP.NET MVC, every controller class name must end with a word "Controller". For example, the home page controller name must be HomeController , and for the student page, it must be the StudentController . Also, every controller class must be located in the Controller folder of the MVC folder structure.
There is no direct way to call JavaScript function from Controller as Controller is on Server side while View is on Client side and once the View is rendered in Browser, there is no communication between them.
A controller is just a class (for example, a Visual Basic or C# class). The sample ASP.NET MVC application includes a controller named HomeController. cs located in the Controllers folder.
You can get controller's name in javascript this way using razor:
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With