Get current controller in view

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


Solution 1:

I have put this in my partial view:

@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()

in the same kind of situation you describe, and it shows the controller described in the URL (Category for you, Product for me), instead of the actual location of the partial view.

So use this alert instead:

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

Solution 2:

I do it like this:

@ViewContext.RouteData.Values["controller"]