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.
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.
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.
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With