Matthew Verstraete
Matthew Verstraete

Reputation: 6781

@ViewBag vs ViewBag in controller

I downloaded a sample project and noticed the author was using @ViewBag when assigning data to ViewBag inside the controller, in all the examples I have seen ViewBag was used but running the project I downloaded showed that using @ViewBag works find so I am now a bit confused. Is there any difference between using @ViewBag and ViewBag inside the controller?

Upvotes: 0

Views: 379

Answers (2)

user247702
user247702

Reputation: 24212

@ViewBag works in the controller but it's not necessary, and likely a mistake from the author.

The reason it works is described in What's the use/meaning of the @ character in variable names in C#?, @ can be used to escape variable names that are also language keywords.

Upvotes: 3

Alan Tsai
Alan Tsai

Reputation: 2525

Are you sure @ViewBag in the controller? In Razor (the view engine asp .net mvc use) uses @ to indicate you want to write some c# code.

so essentially @ViewBag (use in Razor) and ViewBag (Use in controller) is the same thing.

for some basic razore rules, read this: http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax

Upvotes: 0

Related Questions