0

I am learning ASP.net MVC 4, I already understand the MVC pattern and logic. I completed some tutorials from the internet and I found people mixing up these two notations 'ViewBag' and '@ViewBag' in the Controller-class. So I'm a little bit confused when to use '@ViewBag' and/or 'ViewBag'. Is there a difference between them? Or am I free to choose which notation to use when coding?

Check this Controller class code:

public ActionResult Index()
{
   ViewBag.Weights = new List<string>(weightLst);

   @ViewBag.Statuses = new List<Status>(statuses);

   return View();
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Tassisto
  • 9,877
  • 28
  • 100
  • 157
  • @ViewBag used in Razor format its start with @. – Anand Thangappan Dec 30 '13 at 13:06
  • They are the same thing have a look here as to what the @ means, and other Razor markup: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/ – Charleh Dec 30 '13 at 13:07
  • No, it's not duplicate. Why did you downvote this question? :-) – Tassisto Dec 30 '13 at 13:21
  • If you're seeing this in the controller, in context of the `@` prefixed to a string, what you have is called a `verbatim string literal` which tells the compiler to not interpret the string until the next quote. So for example `var string = @"C:\myfolder";` you don't need to escape the backslash. Other than that the `@` is used as the answers below describe, the beginning of an inline statement in a razor view. – Christopher.Cubells Dec 30 '13 at 13:50
  • 1
    Unfortunately, it does not let me answer your question as already marked duplicate. But actually it is not - your question is about `Controller` and not about `Razor View`. So, simple answer is `@ViewBag` and `ViewBag` in C# are same - so, is any other variable. Example, `int a;` can be refereed to as `@a`. The `@` here is C# way to escape any identifier which could be a keyword - example, `int @while;`. Now we have variable called `while` which is not possible without `@`. In case identifier is not keyword, e.g. `ViewBag`, it just does not matter. – YK1 Dec 30 '13 at 14:46
  • This question could be considered duplicate of [this](http://stackoverflow.com/questions/91817/whats-the-use-meaning-of-the-character-in-variable-names-in-c) and not the one suggested above in the question. – YK1 Dec 30 '13 at 14:53

4 Answers4

4

Take a look on Introduction to ASP.NET Web Programming Using the Razor Syntax:

The @ character starts inline expressions, single statement blocks, and multi-statement blocks

So, @ character is used to add code to page. If you are not in code block, then you should use this symbol for Razor to treat ViewBag as code (i.e. start inline expression). Following code will render value of ViewBag.Title:

 <div>@ViewBag.Title</div>

If you are already in code block, then you don't need @ to treat ViewBag as code:

@foreach(var item in ViewBag.Items) {
   <div>@item</div>
}

If you are not in code block, then without @ Razor will treat ViewBag as simple text. Following code will render "ViewBag.Title" string on the page:

<div>ViewBag.Title</div>
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Although answer is nice, the question is not about `Razor View` which this answer describes. Question is about using `@ViewBag` in C# controller code. – YK1 Dec 30 '13 at 14:54
  • @YK1 actually first revision of this question didn't have mentioning of controller class (you can check in in history, and other answers reflect that). For current edit of question answer will be usage of `@` symbol in Identifiers [C# Specification](http://msdn.microsoft.com/en-us/library/aa664670(v=vs.71).aspx). As stated there identifiers are considered same if they are equal after removing `@` prefix. – Sergey Berezovskiy Dec 30 '13 at 15:37
1

@ViewBag and ViewBag are the same thing: the "@" cames from Razor / code implementation: if you are in a @{} block you don't need to add it before ViewBag.

See more in ASP website.. http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax

Nicolas R
  • 13,812
  • 2
  • 28
  • 57
0

Nothing just usage for each one is difference. See this.

-1

In the View, you can reference the ViewBag using an inline expression that must start with the '@' sign. For example, to display the content of the ViewBag...

 <p>@ViewBag.MyValue

You can also reference (set or get) the ViewBag property value in the Action method of the Controller. For example, to set the value during an Action method...

    public ViewResult Index()
    {
        ViewBag.MyValue = "Test";
        return View();
    }
RyanCJI
  • 464
  • 2
  • 8