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
5
votes
3 answers

Cannot perform runtime binding on a null reference. ViewBag.Title is null

I want to provide a simple general functionality to show a message at the top of a page whenever I need to inform a user that operation is successful. My solution would be to put an object into ViewBag.Info that contains details about a message,…
Celdor
  • 2,437
  • 2
  • 23
  • 44
5
votes
3 answers

Html.ActionLink value of ViewBag

In ASP MVC C# I putted a List(Cars) in the ViewBag.cars, now I want to make an actionlink with the title of each car, like this: @if (ViewBag.cars != null) { foreach (var car in ViewBag.cars) {

@Html.ActionLink(@car.title,…

francisMi
  • 925
  • 3
  • 15
  • 31
5
votes
3 answers

What is really the point of ViewBag?

In an ideal 'hello world' one should pass a strongly typed model back to the view. return View(MyModel); If things get sticky, we can create a ViewModel return View(MyViewModel); ex. MyViewModel MyModel Foo I can avoid creating the whole…
Pinch
  • 4,009
  • 8
  • 40
  • 60
5
votes
2 answers

C# Contains Method

My site has a column that displays the news. The DIV-s with these news which contain the word "prize" must be painted in the green color. (If a person has created a record (a news) without specifying the Prize (without word "Prize"), the green is…
Dan
  • 393
  • 1
  • 4
  • 19
5
votes
2 answers

RenderPartial and ViewBag

I'm trying to modify this code: http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3 To make it to get the ViewBag data for render the View in the PDF. Basically, I need to pass to Html.RenderPartial with a new Context my…
Santiago
  • 2,190
  • 10
  • 30
  • 59
5
votes
1 answer

MVC4 ViewBag or ViewModel or?

I need to send data in the form of a list for two different models in my database to a view in an MVC4 project. Something like this: Controller: public ActionResult Index() { Entities db = new Entities(); ViewData["Cats"] =…
id.ot
  • 3,071
  • 1
  • 32
  • 47
5
votes
1 answer

Convert viewbag to javascript array

I want to get the data from the ViewBag.mytags to a Javascript array, but I was not able to avhice this $(function () { var sampleTags = new Array(); var array = @Html.Raw(Json.Encode(@ViewBag.mytags)); for(var i =0;…
dr ammar
  • 57
  • 1
  • 1
  • 6
5
votes
4 answers

Strongly typed Viewbag items

If I am using a ViewBag which contains a strongly typed object, is there any way on MVC Razor to define (or cast this) so that all the elements of that appear in the IntelliSense? For example lets say I have…
John Mitchell
  • 9,653
  • 9
  • 57
  • 91
5
votes
1 answer

C# get properties of the ViewBag?

I get the whole idea of the fact that ViewBag is dynamic, and that new properties can be "added" to the ViewBag by using and initializing those properties for the first time without compilation issues. However, I'm looking for a way to get all the…
Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187
4
votes
3 answers

ViewBag - Object reference not found - MVC 3

Upon publishing my MVC 3 Web Application to my website I get an error stating an Object reference not set to an instance of an object. The 'error' line is: Line 2: ViewBag.Title = "Index"; This is my Index view: @{ Layout =…
ElveMagicMike
  • 175
  • 2
  • 2
  • 5
4
votes
1 answer

How do I access a ViewBag.Title after it has been set by the underlying View?

Here's the thing. I have a MVC Action, and on that action, I have applied a custom ActionFilterAttribute to get the deserialization working. Now, what I want to do, is set some header based on the ViewBag.Title that is set inside this view. I've…
Anže Vodovnik
  • 2,325
  • 16
  • 25
4
votes
2 answers

Why does an MVC view need to exist in the View directory to work?

I have been writing a cms with MVC being used as the main engine for generating the pages. I am going well, but wanted the ability to create a unique razor template per site and possibly per view if I need to. My rules are that each project has to…
Luke Duddridge
  • 4,285
  • 35
  • 50
4
votes
1 answer

Null TempData when passing data from controller to view MVC

I have the following class in a Controller passing data to a View: public ActionResult ControllerToView(){ ... TempData["example"] = "this is a message!"; ... return Redirect("http://myViewPageLink"); } In my View, I am trying to…
Nicole
  • 107
  • 1
  • 2
  • 12
4
votes
1 answer

Mvc 5 unit tests - set viewbag value of controller

I have an index page which could have a ViewBag value of last search. I want to setup my controller so I'm able to set this ViewBag value before calling my system under test (ProductManagementController) Index action [HttpPost] public async…
nick gowdy
  • 6,191
  • 25
  • 88
  • 157
4
votes
1 answer

RuntimeBinderException when using ViewBag

We are getting RuntimeBinderException for the Viewbag items used in the _Layout.cshtml. We observe these exceptions in the memory profiler. They are not fatal (everything works OK) but annoying and we want to clear them out. For example, the…
Zafer
  • 2,180
  • 16
  • 28