I’m trying to create an HtmlHelper that would allow me to communicate across parent and child views. I got tutorial from here: https://gist.github.com/primaryobjects/8442193.
First I created the class and method like this:
namespace SchoolIn.Helpers
{
public static class ViewBagHelpers
{
public static dynamic GetPageViewBag(this HtmlHelper html)
{
if (html == null || html.ViewContext == null) //this means that the page is root or parial view
{
return html.ViewData;
}
ControllerBase controller = html.ViewContext.Controller;
while (controller.ControllerContext.IsChildAction) //traverse hierachy to get root controller
{
controller = controller.ControllerContext.ParentActionViewContext.Controller;
}
return controller.ViewBag;
}
}
}
In the child view I add a using statement:
@using SchoolIn.Helpers
and
@{Html.GetPageViewBag().PageTitle = "My Custom Property Readable By Parent View"; }
In the parent view I’m trying to do this:
@{ ViewBag.Title = ViewContext.ViewBag.PageTitle }
So I guess I have two problems. The first is I don’t have access to html.ViewBag like the tutorial and in the parent view I can’t do...
ViewContex.ViewBag.
Any idea how I can make this work? Thanks for any help.