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
35
votes
2 answers

ViewBag vs ViewData performance difference in MVC?

I know that ViewData and ViewBag both use the same backing data and that neither are as good as using strongly typed models in most cases. However when choosing between the two is the dynamic nature of ViewBag slower than using ViewData?
user169867
  • 5,732
  • 10
  • 39
  • 56
35
votes
1 answer

Is using ViewBag in MVC bad?

It seem like mvc 3 team decided to bring in a feature for dynamic data exchange between a controller and a view called the viewbag but it is A good thing against the strongly typed view we all know about? What are some of the positive and negative…
Rushino
  • 9,415
  • 16
  • 53
  • 93
34
votes
3 answers

How does ViewBag in ASP.NET MVC work behind the scenes?

I am reading a book on ASP.NET MVC and I'm wondering how the following example works: Example #1 Controller public class MyController : Controller { public ActionResult Index() { ViewBag.MyProperty = 5; return View(); …
hattenn
  • 4,371
  • 9
  • 41
  • 80
33
votes
11 answers

MVC ViewBag Best Practice

For the ViewBag, I heard it was a no-no to use. I would assume have the content from the ViewBag should be incorporated into a view model? Question: Is my assumption above the best practice. (Not to use a ViewBag and second to have it in the view…
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
32
votes
2 answers

Does a child action share the same ViewBag with its "parents" action?

I am confused with this: I have an action ,say Parent ,and in the corresponding view file ,I have called a child action ,say Child ,both Parent and Child actions are in the same controller. and I need the Child action and the Parent action to share…
NextStep
  • 1,035
  • 3
  • 14
  • 18
32
votes
9 answers

How to display a list using ViewBag

Hi i need to show a list of data using viewbag.but i am not able to do it. Please Help me.. I tried this thing: ICollection list = new HobbyHomeService().FetchLearner(); ICollection personlist = new…
user1274646
  • 921
  • 6
  • 21
  • 46
30
votes
5 answers

How can I show a viewbag as html?

OK, quite new to ASP.Net MVC, so I'm sorry if this is a silly question, but how do I go about showing the values of a ViewBag as HTML. For Example, if ViewBag.SomeMessage contains the following…
David Archer
  • 2,008
  • 4
  • 26
  • 31
27
votes
4 answers

How to populate javascript variable with JSON from ViewBag?

I have this Index action: public ActionResult Index() { var repo = (YammerClient) TempData["Repo"]; var msgCol = repo.GetMessages(); ViewBag.User = repo.GetUserInfo(); return View(msgCol.messages); } GetMessages returns a list…
empz
  • 11,509
  • 16
  • 65
  • 106
26
votes
2 answers

Create ViewBag properties based on strings

Is there any way to create and use dynamic properties for ViewBag, based on strings? Something like ViewBag.CreateProperty("MyProperty"); ViewBag.Property("MyProperty") = "Myvalue"; Thank you
bzamfir
  • 4,698
  • 10
  • 54
  • 89
26
votes
8 answers

Alternative to ViewBag.Title in ASP.NET MVC 3

By default the new project template for ASP.NET MVC 3 adds the following to the default layout (masterpage in razor): @ViewBag.Title The view must then contain the following to assign the title of the page, for instance: @{ …
soren.enemaerke
  • 4,770
  • 5
  • 53
  • 80
26
votes
4 answers

Updating PartialView mvc 4

Ey! How I could refresh a Partial View with data out of the Model? First time, when the page loads it's working properly, but not when I call it from the Action. The structure I've created looks like: Anywhere in my View: @{…
Mario Levrero
  • 3,345
  • 4
  • 26
  • 57
22
votes
2 answers

Passing strings with Single Qoute from MVC Razor to JavaScript

This seems so simple it's embarrassing. However, the first question is when passing a value from the new ViewBag in MVC 3.0 (Razor) into a JavaScript block, is this the correct way to do it? And more importantly, where and how do you apply the…
user646306
  • 503
  • 1
  • 5
  • 16
21
votes
4 answers

Storing an Anonymous Object in ViewBag

This is probably a silly question, but I am trying to stuff an anonymous object in ViewBag like so: ViewBag.Stuff = new { Name = "Test", Email = "user@domain.com" }; and access it from a View like so: @ViewBag.Stuff.Name I understand ViewBag is…
one.beat.consumer
  • 9,414
  • 11
  • 55
  • 98
20
votes
1 answer

Setting up the value in ViewBag using Jquery

I just want to ask, is there a way to set up the viewbag value dynamically using jquery? I try this code in my script, $(".btn").on("click",function(){ @ViewBag.Id = $(this).attr("id") }); i dont know if its correct but when i try to run my MVC 3…
19
votes
5 answers

ViewBag vs Model, in MVC.NET

This is more of a generic architectural question: I'm trying to decide if it's ok for my programmers to use "ViewBags" to pass data to views that already accept Models. My personal preference is to avoid ViewBags and build Robust Models containing…
DotNet98
  • 727
  • 3
  • 12
  • 24
1
2
3
47 48