Get current controller in view

Viewed 115088

I have a View - _Edit which lives in News M/V/C.

I reuse the V/M via the CategoryController as:

return PartialView("/Views/News/_Edit.cshtml", model);

How from within the View - _Edit can I alert the controller name?

When I:

alert('@ViewContext. RouteData.Values["controller"].ToString()');

The Value is: News However, the URL is: /Category/foobar

Is there a way to get the value 'Category' to alert? thanks

8 Answers

For anyone looking for this nowadays (latest versions) of ASP.NET Core MVC, you can use:

@Context.Request.RouteValues["controller"].ToString()
Related