最近我在ASP.NET MVC项目的cshtml文件中编码。以下代码是奇怪的,我无法理解为什么Razor仍然需要前缀' @'在@ 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"})
;
}
}
}
根据Unexpected "foreach" keyword after "@" character,@Html.DropDownListFor(x => x.SelectedDepartmentIds[i], Enumerable.Empty<SelectListItem>(), "string", new {@class = "hidden department"})
确实在c#source中,因此不需要在语句之前添加@
。但是,如果没有,则视图不会显示drowdown列表。事实上,在Html之前需要@
,尽管它在c#source中。
任何人都可以告诉我为什么? Unexpected "foreach" keyword after "@" character接受的答案是否会遗漏?