You can use ViewBag . Define a property for it CurrentViewand use it.
public ActionResult Create()
{
ViewBag.CurrentView = "Create";
return View();
}
And in the layout you can read and use it as
<h2>@ViewBag.CurrentView</h2>
Or if you want to get it in a variable
@{
var viewName = ViewBag.CurrentView;
}
If you do not want to explicitly specify the name of the viewbag property, you can write a special action filter for this.
public class TrackViewName : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
ViewResultBase view = filterContext.Result as ViewResultBase;
if (view != null)
{
string viewName =view.ViewName;
if(String.IsNullOrEmpty(viewName))
{
viewName = filterContext.ActionDescriptor.ActionName;
}
view.ViewBag.CurrentView = viewName;
}
}
}
[TrackViewName]
public ActionResult Create()
{
return View();
}