意外的“foreach"“@"后的关键字特点 [英] Unexpected "foreach" keyword after "@" character
问题描述
我在剃刀中完成了部分视图.当我运行它时,我收到以下错误 - Razor 似乎陷入了思考我到处编写代码的想法.
<块引用>@"字符后出现意外的foreach"关键字.一旦进入代码,您就不需要在诸如foreach"之类的结构前加上@"
这是我的观点:
@model IEnumerable<div>@using(Html.BeginForm("Update", "UserManagement", FormMethod.Post)) {@Html.Hidden("UserId", ViewBag.UserId)@foreach(模型中的变量链接){如果(链接.链接){<input type="checkbox" name="userLinks" value="@link.Id" checked="checked"/>@link.Description<br/>} 别的 {<input type="checkbox" name="userLinks" value="@link.Id"/>@link.Description<br/>}}}
在您的 using
块中,Razor 需要 C# 源代码,而不是 HTML.
因此,您应该编写不带 @
的 foreach
.
在 HTML 标签中,Razor 需要标记,因此您可以使用 @
.
例如:
<!-- 标记在这里-->@if (x) {//代码在这里如果 (y) {//更多代码在这里<div><!-- 标记在这里-->@if (z) { }}}
如果你想把代码放在需要标记的地方,或者你想在任何地方写输出,你只需要一个 @
.
要将非类似标签的标记放在需要代码的地方,请使用 @:
或
.
I have a partial view done in razor. When I run it I get the following error - it seems like Razor gets stuck into thinking I'm writing code everywhere.
Unexpected "foreach" keyword after "@" character. Once inside code, you do not need to prefix constructs like "foreach" with "@"
Here is my view:
@model IEnumerable<SomeModel>
<div>
@using(Html.BeginForm("Update", "UserManagement", FormMethod.Post)) {
@Html.Hidden("UserId", ViewBag.UserId)
@foreach(var link in Model) {
if(link.Linked) {
<input type="checkbox" name="userLinks" value="@link.Id" checked="checked" />@link.Description<br />
} else {
<input type="checkbox" name="userLinks" value="@link.Id" />@link.Description<br />
}
}
}
</div>
Inside your using
block, Razor is expecting C# source, not HTML.
Therefore, you should write foreach
without an @
.
Inside an HTML tag, Razor expects markup, so you would use @
.
For example:
<div>
<!-- Markup goes here -->
@if (x) {
//Code goes here
if (y) {
//More code goes here
<div>
<!-- Markup goes here -->
@if (z) { }
</div>
}
}
</div>
You only need an @
if you want to put code where it's expecting markup, or if you want to write output anywhere.
To put non-tag-like markup where it's expecting code, use @:
or <text>
.
这篇关于意外的“foreach"“@"后的关键字特点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!