Mou
Mou

Reputation: 16322

what is difference between @ and @:?

tell me what is difference between @ and @: ?

when one use this sign @: ?

i got one

@foreach (var item in ViewBag.radios as string[])
  {
    @:<input type='radio' name='radiovalue' value='@item' /> @item <br />
  }

in the above code @ sign used like @: foreach then again why people give @: sign before starting html code like below one

@:<input type='radio' name='radiovalue' value='@item' /> @item <br />

when one should use @: sign ?

please discuss with sample code to better understand the usage. thanks

Upvotes: 2

Views: 93

Answers (1)

Sam FarajpourGhamari
Sam FarajpourGhamari

Reputation: 14741

We use @: to explicitly tell razor to switch to text mode. Razor intelligent enough to automatically switch between code and text. But for any reason it could not you could use @: to help razor. Consider this:

@using (Html.BeginForm())
{
    @Html.DisplayNameFor(m => m.Name)@: : @Html.DisplayFor(model => model.Name)
}

in this example if you remove @: from middle of statement razor thinks we use : in C# code but actually we want to use : in html not C#.

Upvotes: 1

Related Questions