0

I have decided to use a common viewModel for a number of my screens. However to add to my flexibility I would like to at least know the name of the controller and action that called the viewModel / view.

Is it possible to get this information and how can I do it?

Thanks

tereško
  • 58,060
  • 25
  • 98
  • 150
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • I think this might answer your question.. http://stackoverflow.com/questions/362514/how-can-i-return-the-current-action-in-an-asp-net-mvc-view – David McLean Nov 24 '11 at 10:23
  • I checked the link. Some of those posts look very old so I am wondering if there is a new way in MVC3? – Samantha J T Star Nov 24 '11 at 10:45
  • I'm not sure if there are any MVC3 specific ways of getting the date I would use the route data. like ViewContext.Controller.ValueProvider["action"].RawValue ViewContext.Controller.ValueProvider["controller"].RawValue. - That's a lie.. I would actually look at passing that information to my view.. rather than my view working it out.. views are in my opinion just for displaying data. – David McLean Nov 24 '11 at 11:48

1 Answers1

0
var action = (string)ViewContext.Controller.ValueProvider.GetValue("action").RawValue;
var controller = (string)ViewContext.Controller.ValueProvider.GetValue("controller").RawValue;
Joao
  • 7,366
  • 4
  • 32
  • 48