Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3 - ViewBag property provides no IntelliSense

I recently installed ASP.NET MVC 3 via web platform installer. I don't have the intellisense support for ViewBag in Razor view. Intellisense works fine with model in Razor view. I tried to rebuild solution, disable ReSharper... but I couldn't get it to work.

Any help would be greatly appreciated.

like image 490
šljaker Avatar asked Jan 17 '11 23:01

šljaker


People also ask

What is the purpose of ViewBag property?

The Microsoft Developer Network writes that the ViewBag property allows you to share values dynamically to the view from the controller. As such, it is considered a dynamic object without pre-set properties.

What is ViewBag in ASP.NET MVC?

The ViewBag in ASP.NET MVC is used to transfer temporary data (which is not included in the model) from the controller to the view. Internally, it is a dynamic type property of the ControllerBase class which is the base class of the Controller class.

Can ViewBag store an object?

ViewBag object allows you to store values using object-properties syntax. ViewBag is a wrapper over ViewData. ViewData and ViewBag values are available only during current request.

How do I pass ViewBag from controller view?

To pass the strongly typed data from Controller to View using ViewBag, we have to make a model class then populate its properties with some data and then pass that data to ViewBag with the help of a property. And then in the View, we can access the data of model class by using ViewBag with the pre-defined property.


1 Answers

The ViewBag property is typed as dynamic, which means that there is no IntelliSense.

ViewBag is an alias/alternative syntax for accessing the ViewData dictionary. The two following lines of code are equivalent:

ViewBag.Message = "My message";
ViewData["Message"] = "My message";

ViewBag offers a slightly terser syntax than ViewData. Also notice that accessing ViewData using string keys also provides no IntelliSense, so you don't really lose any functionality.

One last note is that ViewBag and ViewData use the same backing storage, so that setting a property using one method makes it available using the other method:

ViewBag.Message = "My message";
string message = ViewData["Message"];
// message is now "My message"
like image 197
marcind Avatar answered Oct 05 '22 21:10

marcind