Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the MVC controller name in Javascript function

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.....

like image 677
10K35H 5H4KY4 Avatar asked Dec 08 '13 05:12

10K35H 5H4KY4


People also ask

How to name controller in MVC?

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.

Can we call JavaScript function from controller in MVC?

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.

Where in MVC controller?

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.


1 Answers

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';
like image 80
Sirwan Afifi Avatar answered Sep 19 '22 00:09

Sirwan Afifi