1

I need to be able to get a controller name in my custom helper class.

I have the following:

  1. Layout view which calls a controller: "HelperController", action: "Menu" like so:

@Html.Action("Menu", "Helper")

  1. This calls ActionResult and generages an html:

    @model List<MvcMenuItem>
    @{
    Layout = null;
    }
    @{        
    @Html.Menu(this.ViewContext).ClientId("navMain").AddRange(Model).Render()
    }
    
  2. This calls "public static class MvcMenuExtensions" and in there (in my Render() method) I need to be able to get the controller name I'm currently in, NOT the controller name that is calling MvcMenuExtensions

I've tried this:

string controller = this.ViewContext.RouteData.Values["controller"].ToString();

but this results in controller being "Helper" controller and not the current controller I'm in. Since this "ActionResult" is being called from anywhere on the page (it's in the layout).

Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
ShaneKm
  • 20,823
  • 43
  • 167
  • 296
  • This will probably provide answer to you question: http://stackoverflow.com/questions/4412310/how-to-get-current-controller-and-action-from-inside-child-action – juhan_h Aug 01 '12 at 07:48

1 Answers1

1

answered here: How to get current controller and action from inside Child action? by juhan_h

ControllerContext.ParentActionViewContext.RouteData.Values["action"] 

OR

ViewContext.ParentActionViewContext.RouteData.Values["action"]
Community
  • 1
  • 1
ShaneKm
  • 20,823
  • 43
  • 167
  • 296