Recently I am coding in a cshtml file in a ASP.NET MVC project. Following code is odd and I cannot understand Why does Razor still need prefix '@' before @Html.DropDownListFor?
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new {@class = "form-horizontal", role = "form"}))
{
……
for (var i = 0; i < Model.TotalLevel; i++)
{
if (i != 0)
{
@Html.DropDownListFor(x => x.SelectedDepartmentIds[i], Enumerable.Empty<SelectListItem>(), "string", new {@class = "hidden department"})
;
}
else
{
@Html.DropDownListFor(x => x.SelectedDepartmentIds[i], Model.Departments, "string", new {@class = "department"})
;
}
}
}
According to Unexpected "foreach" keyword after "@" character , @Html.DropDownListFor(x => x.SelectedDepartmentIds[i], Enumerable.Empty<SelectListItem>(), "string", new {@class = "hidden department"})
is indeed inside c# source, so there is no need to add @
before the statement. However, if not, the view didn't display drowdown list. So in fact @
is needed before Html though it's in c# source.
Anyone can tell me why? Is the accepted answer from Unexpected "foreach" keyword after "@" character miss something?