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
18
votes
1 answer

Lifetime of ViewBag elements in ASP.net MVC3

When will the values of the ViewBag be flushed or cleared ?
Naveen Vijay
  • 15,928
  • 7
  • 71
  • 92
18
votes
1 answer

How can I use data placed into a ViewBag by a filter in my Error.cshtml view?

I have an action filter that is responsible for placing some common information into the ViewBag for use by all views in the shared _Layout.cshtml file. public class ProductInfoFilterAttribute : ActionFilterAttribute { public override void …
GBegen
  • 6,107
  • 3
  • 31
  • 52
18
votes
4 answers

MVC: Iterating a Viewbag array in javascript

The goal is to get the data from the ViewBag.Array to a Javascript array. The data is calculated in the controller so I cannot fetch it straight from the database. I need the data to draw a chart with jqplot. Code: for(i = 0; i <…
Esa
  • 1,636
  • 3
  • 19
  • 23
17
votes
5 answers

Creating a class like ASP.NET MVC 3 ViewBag?

I have a situation where I would like to do something simular to what was done with the ASP.NET MVC 3 ViewBag object where properties are created at runtime? Or is it at compile time? Anyway I was wondering how to go about creating an object with…
Alex Hope O'Connor
  • 9,354
  • 22
  • 69
  • 112
17
votes
6 answers

In a simple Viewbag.Title, getting a RuntimeBinderException

I have a really simple ViewBag.Title. Like this: @{ ViewBag.Title = "My Title"; ViewBag.MiniTitle = "Sub - Title"; } Which is being parsed on _Layout.cshtml, on the @ViewBag.Title However, I am getting this exception:…
JohnC1
  • 841
  • 1
  • 12
  • 27
17
votes
3 answers

ViewBag.Title value overrides Model.Title for ASP.NET MVC Editor Template

When I set ViewBag.Title at the top of a Razor template, the Model.Title editor template text box is populated using the value of ViewBag.Title instead of the expected Model.Title value. Calling Html.EditorFor(x => Model.Title) explicitly does not…
Petrus Theron
  • 27,855
  • 36
  • 153
  • 287
16
votes
3 answers

Difference between viewbag and viewstate?

Is there a functional difference between ViewState in Webforms and ViewBag in MVC? They seem to be the "same thing". And can be used in the same ways. I ask because MVC promotes the stateless Web and not stuffing data in the page causing bloat and…
dotnetN00b
  • 5,021
  • 13
  • 62
  • 95
16
votes
2 answers

Store a value in ViewBag from javascript

How can I store a value in the ViewBag accessing it from javascript?
iLemming
  • 34,477
  • 60
  • 195
  • 309
15
votes
3 answers

MVC Passing ViewBag value from another controller

I've set a value into Viewbag.message in my default HomeController and successfully display it in my Shared/_Layout.cshtml. After that I added another TestController and in the TestController view, Viewbag.message seems to be null. May I know…
SuicideSheep
  • 5,260
  • 19
  • 64
  • 117
15
votes
3 answers

Passing list from MVC ViewBag to JavaScript

I have a list of users which I pass from my controller to my view using the view bag. Now I need to be able to pass the same list to the javascript on the page. I could reconstruct the list using a foreach loop: @foreach (var item in…
Matt
  • 3,820
  • 16
  • 50
  • 73
13
votes
5 answers

c# mvc model vs viewbag

Suppose you have a list of People A and a list of People B in a page. And these two are seperate classes in L2S, representing two different tables. Therefore, you cannot pass a single model as follows: ... @model PeopleA ... @foreach(var peopleA in…
Shaokan
  • 7,438
  • 15
  • 56
  • 80
13
votes
3 answers

How to set Dynamic title of @Html.ActionLink from Controller?

i have @Html.ActionLink("Remove 1034, 1035, 1036", "RemoveSelected") Now i want to set each id from Controller for example: @Html.ActionLink(ViewBag.RemoveSelectedTitle, "RemoveSelected") //this is not work // GET: /TabMaster/ public…
imdadhusen
  • 2,493
  • 7
  • 40
  • 75
12
votes
1 answer

Clear the ViewBag?

Is there any way to clear the ViewBag? ViewBag has no setter, so it can't simply be nulled out: ViewBag = null; I also can't seem to use reflection to iterate over it and wipe out its dynamic properties, as you can't create an instance of a…
Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
12
votes
1 answer

How do I send assign a List to a JavaScript array or enumerable object

I have the following : ViewBag.SomeEnumerable = new List() { "string1", "string2" }; Now how do I assign ViewBag.SomeEnumerable to an array or some form of enumerable object on the JavaScript side? e.g.: function SomeFunction() { var…
Eminem
  • 7,206
  • 15
  • 53
  • 95
12
votes
3 answers

MVC3 RedirectToAction in a post method and ViewBag suppression

i'm currently working a list of data that i need to display in a view that represent a list and show for each item the corresponding action that can be executed, like edit them or delete them. For the edition there is no problem concedering that…
Oflocet
  • 566
  • 3
  • 9
  • 19
1 2
3
47 48