8

I'm currently trying to pass dynamic data from my controller to my view AND layout. I'm currently using the ViewBag to pass data from the Controller to the view which works fine. I'm not able to access the ViewBag from within the layout. Is there a way to do this?

I'm using ASP.NET W/ MVC5 (C#).

Edit: Updated W/ code:

//layout.cshtml
@ViewBag.username

Yields this error:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'ViewBag' does not exist in the current context
tereško
  • 58,060
  • 25
  • 98
  • 150
jbq
  • 181
  • 3
  • 4
  • 13
  • 1
    Could you attach some snippets of code that you tried? – Oleksii Aza Nov 25 '13 at 22:15
  • You should be able to do that. Posting your code would help. – sakura-bloom Nov 25 '13 at 22:17
  • 1
    Is there Web.config in your `Views` directory? Some information here: http://stackoverflow.com/questions/15550899/the-name-viewbag-does-not-exist-in-the-current-context and http://stackoverflow.com/questions/4960148/the-name-viewbag-does-not-exist-in-the-current-context – sakura-bloom Nov 25 '13 at 22:27
  • Can you post a detailed version of layout.cshtml ? Also did you try to run it after building the project once ? – Shyju Nov 25 '13 at 22:28
  • The issue was that my layout folder wasn't located inside the views folder. Thanks – jbq Nov 25 '13 at 22:33

4 Answers4

12

I would suggest that you have an action method, which the layout uses, that passes the data you need. For example

public class ControllerName : Controller
{
    public ActionMethod GetData()
    {
       return Content("Some data"); // Of whatever you need to return.
    }
}

then in the layout and the view you can call

@Html.Action("GetData", "ControllerName")
Jason Evans
  • 28,906
  • 14
  • 90
  • 154
2

An easy way is to set a ViewBag property from your view, which will then be available in your Layout file

Index.cshtml

@{
   ViewBag.Container = false;
}

_Layout.cshtml

@{
   var container = ViewBag.Container != null && !ViewBag.Container ? "" : "container";
}

<div class="@container">
   ...
</div>
Hazza
  • 6,441
  • 3
  • 24
  • 37
  • Then we will have to add ViewBag to all Views – Fatih TAN Oct 03 '18 at 06:42
  • I think ViewBag should be used as little as possible. The example I presented is one use I have found for it, where I want a select few pages to not have the bootstrap container class applied so can be full width. This means I don't have to clutter my views with containers, and it is perfect at the view level. It depends what data you want to send to your Layout I suppose – Hazza Oct 03 '18 at 10:33
1

Consider an Action in AppController controller which returns Index.cshtml view :

public class AppController: Controller
 {
    public IActionResult Index()
    {
       ViewBag.Title = "Home Page";
       return View();
    }
}

In my Index.cshtml view I can directly call @ViewBag.Title to get the content what I set in the action before.

<head>
    <meta charset="UTF-8" />
    <title>@ViewBag.Title | Example.com</title>
    <link href="css/site.css" rel="stylesheet" type="text/css" />
</head>

You can even pass complex data like list and access those in view with Razor's model/Model.

I hope I helped you with this answer.

Abhay Shiro
  • 3,431
  • 2
  • 16
  • 26
-1

Simply use the viewbag in your home controller and you can also access it on the Layout page too.

M Asif
  • 1