我用剃刀做了部分视图.当我运行它时,我得到以下错误 - 看起来Razor似乎一直在想我到处都在编写代码.
"@"字符后出现意外的"foreach"关键字.进入代码后,您不需要使用"@"作为"foreach"的结构前缀
这是我的观点:
@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>
Run Code Online (Sandbox Code Playgroud) 我最近将一个项目从MVC 1升级到MVC 3,现在我正在尝试使用Razor.
在一个视图中,我有一个foreach代码块,但嵌套的if语句似乎不希望它前面的@.
我原来的代码是:
@foreach(var r in Model.Results)
{
string css = r.Result.Count() > 0 ? "fail" : "pass";
<p class="@css"><strong>@r.Description</strong></p>
@if(r.Result.Count() > 0)
{
<p>Count: @r.Result.Count()</p>
<table>
<thead>
<tr>
<th>ID</th><th>Title</th><th>Description</th>
</tr>
</thead>
<tbody>
@foreach(var e in r.Result) {
<tr><td>@e.Id</td><td>@e.Title</td><td>@e.Description</td></tr>
}
</tbody>
</table>
}
}
Run Code Online (Sandbox Code Playgroud)
我将在@if中遇到运行时错误:在"@"字符后面出现意外的"if"关键字.进入代码后,您不需要使用"@"作为"if"等构造的前缀.
如果我删除@代码运行正常.我期望需要@因为它前面的HTML.更令我困惑的是,在嵌套的foreach之前我确实需要@.这里有什么规则?