user10099944
user10099944

Reputation:

ViewBag in View without @

I don't get why we sometimes use ViewBag without reference (I mean @) to Controller in View, e.g.:

@{
    string urlFilter = Url.Action("Index", "Home", new
    {
        CustID = ViewBag.custid,
        Errors = ViewBag.errors
    });}

It looks like a part of c# code in view. I know that razor synthax allow us to inject c# code into View but don't understand what's the point of using ViewBag without @ in View

Upvotes: 0

Views: 41

Answers (1)

Gavin H
Gavin H

Reputation: 10502

In this case it is because it is within the scope of a C# code block (@{ ... }) and not in the HTML markup.

If however, you were trying to reference the ViewBag inline in an HTML block you would need to prefix it with @ to make sure it was processed by the Razor engine.

for example:

<p>@ViewBag.Name</p>

ViewBag is a dynamic property on the WebPageView from which the view is derived.

You can learn about the Razor syntax here: https://learn.microsoft.com/en-us/aspnet/web-pages/overview/getting-started/introducing-razor-syntax-c

Upvotes: 0

Related Questions