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

Why can Razor not see ViewBag, Url.Content, etc. when the layout page is outside ~/Views/Shared?

I'm working on a CSS framework that I can drop in to other projects as a NuGet package. To keep things clean, the entire framework - views, styles, images, and master pages / layout pages - is actually stored under ~/CssThing/ Works perfectly with…
Dylan Beattie
  • 53,688
  • 35
  • 128
  • 197
10
votes
3 answers

using viewbag with jquery - asp.net mvc 3

I have a ViewBag.IsLocal set to true in controller. I would like to use jquery to check the ViewBag value and display an alert. Code: if(@ViewBag.IsLocal == true) { alert("yeah"); } I never get the alert. When I use Firebug to see the value of…
bobek
  • 8,003
  • 8
  • 39
  • 75
10
votes
2 answers

Late binding issue with MVC3 ViewBag in VB.NET

I'm trying out MVC Scaffolding in a VB.NET MVC3 project and running into an issue with late binding with Option Strict set on (and I want it on). This works in C#: public ActionResult Create() { ViewBag.PossibleTeams = context.Teams; return…
CrispinH
  • 1,899
  • 4
  • 23
  • 38
10
votes
3 answers

Select a default value in dropdownlistfor MVC 4

I'm trying to make a dropdownlistfor with a selected value but it doesn't work :/ And I search on the web but I don't find the solution :/ For the moment, I'm doing this : In C# : ViewBag.ID_VEH = new SelectList(db.VEHI, "ID_VEH", "COD_VEH", 4); //…
user2609766
  • 101
  • 1
  • 1
  • 3
9
votes
5 answers

How to get bool value from ViewBag on view?

I am trying to receive bool values from ViewBag. But the issue is that to receive value I am using: var test = '@ViewBag.Test' In this way value will be 'True' or 'False' (string). Of cource I can use something like if (test == 'True') but, is…
Olegs Jasjko
  • 2,128
  • 6
  • 27
  • 47
9
votes
1 answer

mvc c# html.dropdownlist and viewbag

So I have the following (pseudo code): string selectedvalud = "C"; List list= new List(); foreach(var item in mymodelinstance.Codes){ list.Add(new SelectListItem { Text = item.Name, Value = item.Id.Tostring(),…
gdubs
  • 2,724
  • 9
  • 55
  • 102
8
votes
4 answers

MVC3 putting a newline in ViewBag text

I have an MVC3 C#.Net web app. I am looping through a DataTable. Some rows are importing OK, some are not. I am wanting to send a list of errors back to the view in a list format. I am assigning the following text to a ViewBag property Input…
MikeTWebb
  • 9,149
  • 25
  • 93
  • 132
8
votes
2 answers

Can ViewBag be null in MVC4?

In MVC4, is it possible for ViewBag to ever be null? If so, under what scenarios could it be? If not, how do you know? Specifically, I want to know if it can be null in a view. However, additional information about whether it can be null in a…
matthewpavkov
  • 2,918
  • 4
  • 21
  • 37
8
votes
1 answer

What is the best way to create dropdownlists in MVC 4?

I want to know,What is a best way to create dropdownlists in MVC 4? With ViewBag or another approach?
8
votes
3 answers

How can ViewBag data be saved after a form post?

So I have a ViewBag.Something and this data is randomly generated. In my view, I set this to a label like so @Html.LabelFor(m => m.Something, (string)ViewBag.Something). This works out well but when I submit the form there could be errors and if…
Pittfall
  • 2,751
  • 6
  • 32
  • 61
7
votes
1 answer

MVC Pass ViewBag to Controller

I have a Viewbag that is a list that I am passing from the Controller to the View. The Viewbag is a list of 10 records in my case. Once in the view, if the user clicks on save, I like to pass the content of the View to the [HttpPost] Create…
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
7
votes
4 answers

How can my ViewData be null, but expandable in the debugger?

Just came across an interesting effect while debugging a View. The scenario is easy to reproduce - I have a breakpoint in a View, in the Watch window I add ViewBag.ViewData and the value is null. However, if I just add ViewBag and expand the object,…
Yakimych
  • 17,612
  • 7
  • 52
  • 69
7
votes
3 answers

Can I use ViewBag in an .aspx page?

I have to move my UI page from a .cshtml file to an .aspx file. Now I'm having a couple of compiling errors. First is that 'ViewBag' does not exist in the current context. Can I not use it in .aspx? If not, what is a good substitute? Second, the…
ericgilchrist
  • 262
  • 4
  • 10
7
votes
5 answers

Setting text input's value from ViewBag? ASP.NET MVC5

I am passing a variable ViewBag.crimeRef from my controller and am using it to display a message which works fine: @if(ViewBag.crimeRef != null) {
7
votes
3 answers

NullReferenceException while accessing ViewBag in Asp.net MVC4

I am trying to access the ViewBag data in my view as below: @ViewBag.BreadCrumb I am sending that ViewBag data from code like: ViewBag.BreadCrumb = topic.Category.CatName + " / " + topic.Name; ViewBag.TopicID =…
rughimire
  • 374
  • 1
  • 5
  • 16