2

I apologize in advance for the noob question. I am used to Classic ASP and am testing the water with .Net ASP Web Pages (cshtml).

I created a cshtml page (Index.cshtml) that will be including a header and a footer. I am including them using

@RenderPage("~/Shared/_Header.cshtml")
@RenderPage("~/Shared/_Footer.cshtml")

At the top of the Index.cshtml, I want to set a variable:

var topNav = "services";

I would like to access this variable from the header and/or footer. How can I accomplish this?

xweb
  • 115
  • 3
  • 10
  • Use a [ViewBag](http://www.codeproject.com/Articles/476967/WhatplusisplusViewData-cplusViewBagplusandplusTem) or a strongly typed model. – keenthinker Jul 08 '14 at 21:42
  • @pasty - I would not recommend using ViewBag as it is not strongly typed. – Dangerous Jul 08 '14 at 21:44
  • @xweb - What is it that you are trying to achieve by passing in the word "services" to the header or footer section? – Dangerous Jul 08 '14 at 21:47
  • @xweb - Also, are the header and footer sections displayed in other pages as well as the Index? If so they may be more suited to being placed in a layout view. – Dangerous Jul 08 '14 at 21:48
  • @Dangerous - Remember, I'm coming from Classic ASP here. I typically set variables in the main page, and access them from the included pages, like the header/footer. In this example, the header file has a navigation menu (Home | Services | Contact | ETC). If I am on a specific page (or subset of pages) I highlight a navigation element. In this case, if the variable topNav = "services" I can highlight the "services" navigation element. If topNav = "contact" I can highlight "contact". – xweb Jul 08 '14 at 21:50
  • I apologize for tagging this question incorrectly. On a side note, I believe I can use The Page Object to accomplish this. – xweb Jul 08 '14 at 23:17

2 Answers2

3

Updated: Since this is not a MVC4 site, but standalone Razor templates, the standard answers for MVC don't apply. This page shows how to pass data between pages using the PageData object Customizing Site-Wide Behavior

My original answer doesn't really apply, but the concepts are still valid:

There are at least four objects that can be used to accomplish this. Perhaps the easiest would be ViewBag. See When to use ViewBag, ViewData... for more specifics.

@{
    ViewBag.topNav = "services";
}

You may find that a bit problematic, since ViewBag does not provide strongly typed properties. if ViewBag.topNav does not exist, it will return null when accessed.

However, none of the containers in the article will provide type checking. This brings me to the fourth object which can handle this. That is the model object which is passed to the view. See Using ViewModels for more specifics.

public class IndexViewModel {
   public string TopNav { get; set; }
   // other view properties here
}

In your controller:

public ActionResult Index() {
   var model = new IndexViewModel {
      TopNav = "services",
      // set other properties here
   }
   return View(model);
}

And in the index view:

@model IndexViewModel

@RenderPage("~/Shared/_Header.cshtml",Model)

_Header.cshtml:

@model IndexViewModel
<h2>@Model.topNav</h2>

One last item to note is to consider using a Shared Layout.cshtml instead of embedding the header and footer in each page.

B2K
  • 2,541
  • 1
  • 22
  • 34
  • @B2K- Thanks for the detailed response. I tried the first option and received an error "The name 'ViewBag' does not exist in the current context". In your second example, where do I place the "public class IndexViewModel" code and "public ActionResult Index" code? I do not have a "controller" at this point. – xweb Jul 08 '14 at 22:14
  • In your MVC solution, you should see a Controllers directory with a file called HomeController.cs For where to put the ViewModel, see the article that I referenced. – B2K Jul 08 '14 at 22:27
  • I dont believe "ASP.NET Web Pages" contain these files. There are no .cs files. http://www.asp.net/web-pages/tutorials/introducing-aspnet-web-pages-2/getting-started – xweb Jul 08 '14 at 22:42
  • You aren't using MVC4. Because your question was tagged incorrectly, we've all assumed you were using ASP.Net MVC4. Here is a page that will show you how to use a [layout](http://www.asp.net/web-pages/tutorials/introducing-aspnet-web-pages-2/layouts) in your site. Further tutorials exist on that site to help you. – B2K Jul 08 '14 at 23:08
  • See my update about using PageData. I think that's what you want, but I've got no way to test it. – B2K Jul 08 '14 at 23:25
1

One of the options is to use ViewBag:

http://weblogs.asp.net/hajan/viewbag-dynamic-in-asp-net-mvc-3-rc-2

vmg
  • 9,920
  • 13
  • 61
  • 90
  • 1
    @Vitaly - Where would I place this ViewBag code? I have 3 files. index/header/footer.cshtml. – xweb Jul 08 '14 at 21:58