I have a view filed called Display.cshtml and a partial view called _AppMenu.cshtml. In the _AppMenu.cshtml,how would I pragmatically retrieve the file name for "Display.cshtml" the file that rendered it? The methods presented in the possible answer require that the action name is the same as the view which is not always true in my case.
Asked
Active
Viewed 417 times
0
-
`@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString() + ".cshtml"` – Sani Huttunen Jul 18 '18 at 20:05
-
1Possible duplicate of [How to get parent view from partial view](https://stackoverflow.com/questions/11900820/how-to-get-parent-view-from-partial-view) – Sani Huttunen Jul 18 '18 at 20:07
-
Or you could try setting `ViewData` or `Viewbag` in "Display.cshtml" or all other views you have then read that in "_AppMenu.cshtml" – Timothy Macharia Jul 18 '18 at 20:22
-
ViewData or Viewbag is not reliable enough there is a possibility that the values are not set and this is very manual. Sani, this only works if the view name matches the controller name. – cloutcomputer Jul 18 '18 at 20:27
-
This sounds suspiciously like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What problem are you trying to solve by getting the file name? – Jasen Jul 18 '18 at 20:50
-
Possible duplicate of [Access Parent Model from partial view](https://stackoverflow.com/questions/15322180/access-parent-model-from-partial-view) – Thymine Jul 18 '18 at 22:42
-
@Jasen the thing about the XY Problem is that its diagnosis is often an XY Problem itself. – user2880616 Jul 19 '18 at 19:38