Questions tagged [viewbag]

ViewBag is a dynamic type object in the ASP.NET MVC framework that can be used to send miscellaneous data from the controller to the view.

ViewBag is a member of the ControllerBase class in the ASP.NET MVC framework. It's a dynamic type object that can be used to send miscellaneous data from the controller to the view. Its type is dynamic.

718 questions
366
votes
17 answers

What's the difference between ViewData and ViewBag?

I saw the ViewBag in MVC 3. How's that different than ViewData in MVC 2?
user469652
  • 48,855
  • 59
  • 128
  • 165
193
votes
7 answers

How to create own dynamic type or dynamic object in C#?

There is, for example, the ViewBag property of ControllerBase class and we can dynamically get/set values and add any number of additional fields or properties to this object, which is cool. I want to use something like that, beyond MVC application…
Dmytro
  • 16,668
  • 27
  • 80
  • 130
105
votes
7 answers

How ViewBag in ASP.NET MVC works

How does the ASP.NET MVC's ViewBag work? MSDN says it is just an Object, which intrigues me, how does "Magic" properties such as ViewBag.Foo and magic strings ViewBag["Hello"] actually work? Also, how can I make one and use it in my ASP.NET WebForms…
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
93
votes
16 answers

The name 'ViewBag' does not exist in the current context

I am trying to use ViewBag in my application, I have all of the recent dlls, the latest version of MVC 3, but yet I am still getting the Error: "The name 'ViewBag' does not exist in the current context" I have even uninstalled and then…
efleming
  • 1,269
  • 2
  • 10
  • 10
72
votes
2 answers

How do I render HTML from the Viewbag using MVC3 Razor

I am trying to pass a form element into an MVC3 view by using the Viewbag and simply write the HTML to the page ... In controller: ViewBag.myData = ""; In view (I know I could use a…
Sunrise
  • 1,455
  • 2
  • 16
  • 13
60
votes
8 answers

Can't access ViewBag in a partial view in ASP.NET MVC3

I have a controller calling a view. In the view there is a PartialView called be @Html.Partial("ViewName", model). This works fine. But in the controller I wish to put something in the viewbag what would be hard to put in the viewmodel I pass to the…
vinczemarton
  • 7,756
  • 6
  • 54
  • 86
49
votes
6 answers

Pass a simple string from controller to a view MVC3

I know this seems pretty basic, and it should be, but I can't find out where I am going wrong. (I hve read other articles with similar titles on SO, and other resources on the web but still cant figure it out). I have a controller and in it I am…
Francis Rodgers
  • 4,565
  • 8
  • 46
  • 65
44
votes
10 answers

Modifying MVC 3 ViewBag in a partial view does not persist to the _Layout.cshtml

I am using MVC 3 with the Razor view engine. I want to set some values in the ViewBag inside a Partial View and want retrieve those values in my _Layout.cshtml. For example, when you setup a default ASP.NET MVC 3 project you get a _Layout.cshtml…
Zoran
  • 844
  • 1
  • 7
  • 15
42
votes
9 answers

How to use a ViewBag to create a dropdownlist?

Controller: public ActionResult Filter() { ViewBag.Accounts = BusinessLayer.AccountManager.Instance.getUserAccounts(HttpContext.User.Identity.Name); return View(); } View: Account: @Html.DropDownListFor("accountid", new…
rikket
  • 2,357
  • 7
  • 46
  • 74
40
votes
4 answers

ViewBag/ViewData Lifecycle

I have seen many posts about when to use ViewBag/ViewData vs ViewModel but i have not been able to find an explanation of the lifecycle of the ViewBag. For example, i have two Action methods in one Controller: // POST:…
JTech
  • 3,420
  • 7
  • 44
  • 51
39
votes
3 answers

Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject'

When I try to assign a value to the ViewBag I get the following error: Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject' My code is as follows: public ActionResult Success() { ViewBag["SuccessBody"] =…
RealityDysfunction
  • 2,609
  • 3
  • 26
  • 53
39
votes
2 answers

ViewModels or ViewBag?

I'm fairly new to MVC4, EF5 and ASP.Net, and I don't seem to be able to find a good answer anywhere. Basically, Should everything be done through the viewmodel or is it Ok to also incorporate viewbag? Say I have a method which populates a drop down…
Riddick
  • 1,278
  • 4
  • 17
  • 24
38
votes
7 answers

How do I access ViewBag from JS

My attempted methods. Looking at the JS via browser, the @ViewBag.CC is just blank... (missing) var c = "#" + "@ViewBag.CC"; var d = $("#" + "@ViewBag.CC").value; var e = $("#" + "@ViewBag.CC").val(); var c =…
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
36
votes
3 answers

Viewbag check to see if item exists and write out html and value error

I'm using razor syntax and I want to check to see if certain ViewBag values are set before I spit out the html. If a value is set then I want to write it out. If not I want it to do nothing. @if (ViewBag.UserExists != null) {…
Dietpixel
  • 9,983
  • 11
  • 28
  • 33
35
votes
6 answers

Checking to see if ViewBag has a property or not, to conditionally inject JavaScript

Consider this simple controller: Porduct product = new Product(){ // Creating a product object; }; try { productManager.SaveProduct(product); return RedirectToAction("List"); } catch (Exception ex) { ViewBag.ErrorMessage = ex.Message; …
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
1
2 3
47 48