šljaker
šljaker

Reputation: 7374

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.

Upvotes: 12

Views: 12736

Answers (2)

ajma
ajma

Reputation: 12206

Adding to marcind's answer of the ViewBag being dynamic:

If you want intellisense, then you're going to have to pass in a strongly typed object and then in your view, you can set:@model Namespace.YourModel which will give you intellisense when you try to do @Model.Property

Upvotes: 9

marcind
marcind

Reputation: 53181

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"

Upvotes: 39

Related Questions