0

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.

CloudyKooper
  • 719
  • 2
  • 16
  • 41
  • What is it that your trying to pass back from the partial to the view? Are you sure that you need to do this or could there be a better way of designing your layout/view/partials/models? – Dangerous May 17 '14 at 00:31

1 Answers1

1

I managed to get it working after a couple of tweaks. Make sure that your syntax is correct and that your title is set after the partial is rendered.

Layout Page (_Layout.cshtml):

@using SchoolIn.Helpers

@* Nb. The example did not include the call to partial. *@
@Html.Partial("_Child")

@* The following line must appear after the partial is rendered for the ViewBag to have been set. *@
@* Nb. The example was missing the semicolon. *@
@{ ViewBag.Title = ViewContext.ViewBag.PageTitle; }

Partial View (_Child.cshtml):

@using SchoolIn.Helpers

@{Html.GetPageViewBag().PageTitle = "My Custom Property Readable By Parent View"; }
Dangerous
  • 4,590
  • 3
  • 29
  • 47
  • No error message but the title does not show either. I found I needed to do this to access the ViewBag property: .ViewContext.Controller.ViewBag in the helper file and in the views. Also my partial view is called using @Ajax.ActionLink(). I wonder if this has anything to do with anything. – CloudyKooper May 17 '14 at 15:57
  • You never mentioned in your question that you were using Ajax, but yes, if you are using Ajax then this method to set the title will not work. The method you are using sets the title in the ViewBag which is only available on the server side. Once that view has been sent to the browser it no longer has access to the ViewBag and thus the title cannot be set using your method. – Dangerous May 19 '14 at 09:17
  • If you are using Ajax to retrieve the partial then you could use [JavaScript to change the title](http://stackoverflow.com/questions/413439/how-to-dynamically-change-a-web-pages-title) at the time the page is retrieved although this is not recommended for SEO purposes. – Dangerous May 19 '14 at 09:17