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

Assign Attribute to @Html.DropdownList

I want to assign some ID attribute name to my Dropdown list which is using data from the ViewBag as, ViewBag.Group = new SelectList(group, "GroupId", "Name"); and in the View side I have used Razor syntax for showing the Dropdown…
Rahul RJ
  • 237
  • 3
  • 7
  • 15
7
votes
4 answers

Pass image from controller and display in a view using ViewBag in ASP.NET MVC 3

I guess it's something very straight forward but I can't find out how to do it. In my controller I have: public ViewResult ShowForm() { ViewBag.Title = Resources.ApplicationTitle; ViewBag.LabelStatus =…
Leron
  • 9,546
  • 35
  • 156
  • 257
7
votes
3 answers

Effectively avoiding ViewBag in ASP.NET MVC

How do you deal with avoiding ViewBag due to its risk of error with being dynamic but also avoid having to populate a new ViewModel and pass it back to the view each time. For instance, I don't want to necessarily change the follow to expose common…
Scott
  • 658
  • 6
  • 16
7
votes
3 answers

MVC - use C# to fill ViewBag with Json Action Result

I have an MVC website with C# code behind. I am using an ActionResult, that returns Json. I am trying to put something in the ViewBag but it doesn't appear to work. The code looks like this - public ActionResult GetStuff(string id) { …
A Bogus
  • 3,852
  • 11
  • 39
  • 58
7
votes
3 answers

Asp.Net MVC layout and partial views

let's consider two views that use the same layout composed of: A left column containing a "body" (which is filled differently by both views) A right column that displays general information (passed via the model) Instead of defining the right…
Timothée Bourguignon
  • 2,190
  • 3
  • 23
  • 39
6
votes
2 answers

Heavy use of ViewBag

I make heavy use of the ViewBag in my MVC application, is this considered bad practice? I'm unsure as to whether to spend time creating ViewModels (however I thought that was more suited to MVVM rather than MVC) or continue using the ViewBag…
Paul
  • 3,072
  • 6
  • 37
  • 58
6
votes
1 answer

How to unit test ViewBag in ASP.NET Core?

During writing unit tests for ASP.NET Core application I realized that the ViewData is accessable from ViewResult but ViewBag is not. My HomeController: public class HomeController : Controller { private readonly IHeroRepository _heroItems; …
Prolog
  • 2,698
  • 1
  • 18
  • 31
6
votes
1 answer

pass model to view using viewbag

i want to send two model in my view and in other page maybe and need more controller ViewBag.Users = db.Users.ToList(); and in View @using Documentation.Models @{ var Users = (Documentation.Models.User)ViewBag.Users; } @foreach (var…
Pnsadeghy
  • 1,279
  • 1
  • 13
  • 20
6
votes
1 answer

DropDownList From Objects MVC

Ok so I want make a DropDownList from my object list here is my getting object method public List GetCategoriesList() { BaseShopEntities context = new BaseShopEntities(); List uniqCategories = (from c in context.Category …
Dox
  • 158
  • 1
  • 10
5
votes
2 answers

Razor Layout from another assembly

Is it possible to use a layout from another assembly? I spitted up my application in different layers and have a separated UI layer which contains the master layout for the designers to edit. Is it possible and what I what would my reference looks…
René Stalder
  • 2,536
  • 5
  • 31
  • 50
5
votes
1 answer

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

I am working over my first application over MVC3 and still kind of a newbie in it: I’m trying to success my ViewData[] over a master page because its contains a message that would be used over every page, but when I’m trying to access that it…
Maven
  • 14,587
  • 42
  • 113
  • 174
5
votes
1 answer

Is there a way to access a .NET MVC ViewBag's properties using a string key?

I have a value that I've put in the ViewBag: ViewBag.SomeKey = value; In my view I have the key stored as a String. String theKey = "SomeKey"; Is there a way to access my value from the Viewbag using this String version of the key?
Richard Garside
  • 87,839
  • 11
  • 80
  • 93
5
votes
1 answer

ASP.NET MVC 3 Model Binding - ViewBag.Title clash with input of id="Title"

There seems to be an issue with the ViewBag dynamic properties. Lets say I have: @{ ViewBag.Title = @Model.CourseName; } And then in a form on the page I have: @Html.TextBox("Title", null, new {style="width:400px;"}) Where Title is the name…
awrigley
  • 13,481
  • 10
  • 83
  • 129
5
votes
1 answer

Accessing ViewBag list via Javascript

I'm trying to access the elements of a list stored in ViewBag as follows: function equipamentoTemControle() { for(i = 0; i < @ViewBag.qtdEquipamentos; i++) { var contratocod = @ViewBag.DadosEquipamentos[i].contratocod; } } But when…
5
votes
1 answer

Passing a model object through ViewBag

I was wondering if it is possible to pass a model object through ViewBag. I tried the following codes but somehow in my View, it is only the path of the model which is displayed. Controller: public ActionResult Tempo() { DateTime date1 = new…
refresh
  • 1,319
  • 2
  • 20
  • 71