Asp.net mvc 4 MVC Razor Foreach循环(两次)不';不工作,括号错误
鉴于:Asp.net mvc 4 MVC Razor Foreach循环(两次)不';不工作,括号错误,asp.net-mvc-4,razor,foreach,brackets,Asp.net Mvc 4,Razor,Foreach,Brackets,鉴于: //一个强类型视图模型,其中数据完全传入。 @DisplayNameFor(model=>model.Name) @DisplayNameFor(model=>model.Area) @DisplayNameFor(model=>model.Hours) @DisplayNameFor(model=>model.Minutes) @DisplayNameFor(model=>model.PercentOfTotal) @foreach(名称中的当前变量) { @当前 //请注意,这是问题
//一个强类型视图模型,其中数据完全传入。
@DisplayNameFor(model=>model.Name)
@DisplayNameFor(model=>model.Area)
@DisplayNameFor(model=>model.Hours)
@DisplayNameFor(model=>model.Minutes)
@DisplayNameFor(model=>model.PercentOfTotal)
@foreach(名称中的当前变量)
{
@当前
//请注意,这是问题所在。
//我在另一个里面有一个foreach。Razor似乎认为我需要另一个“}”
//但是编译器告诉我在上面的第1行需要它
var stuff=@Model.Where(p=>p.Name==current);
foreach(stuff中的var项)
{
@项目.面积
@项目.时间
@项目.会议记录
@项目.总百分比
}
}
无论我尝试什么,剃须刀都不喜欢打圈!除非我做错了什么。。。如果忽略设计器错误并运行它,运行时会告诉我需要另一个“}”,除非我真的很累,否则我只能看到缺少括号的地方。删除第二个foreach时没有编译器错误,但添加第二个foreach后就会出现问题。删除
@模型中的@
。
//A Strongly typed view model where the data is passed in perfectly.
<table id="dt1">
<thead>
<tr>
<th>@Html.DisplayNameFor(model => model.Name)
</th>
<th>@Html.DisplayNameFor(model => model.Area)
</th>
<th>@Html.DisplayNameFor(model => model.Hours)
</th>
<th>@Html.DisplayNameFor(model => model.Minutes)
</th>
<th>@Html.DisplayNameFor(model => model.PercentOfTotal)
</th>
</tr>
</thead>
@foreach (var current in names)
{
<tr>
<td>@current</td>
</tr>
//Notice this is the problem area.
//I have a foreach inside of another one. Razor seems to think I need another "}"
//But the compiler tells me I need it at line 1 above
var stuff = @Model.Where(p => p.Name == current);
foreach (var item in stuff)
{
<tr>
<td>@item.Area</td>
<td>@item.Hours</td>
<td>@item.Minutes</td>
<td>@item.PercentOfTotal</td>
</tr>
}
}
</table>
要了解更多信息,您也可以这样写
....
var stuff = Model.Where(p => p.Name == current);
foreach (var item in stuff)
{
....
及
@for(int i=0;i
数据库和EF提供了更好的性能。
因为如果您不想更改数据,连接越少越好
p、 (对不起我的英语,我正在学。)
var stuff = Model.Where(p => p.Name == current);
@foreach (var item in stuff)
{
<tr>
<td>@item.Area</td>
<td>@item.Hours</td>
<td>@item.Minutes</td>
<td>@item.PercentOfTotal</td>
</tr>
}
Model.AsNoTracking().Where(p => p.Name == current).ToList()
@for (int i = 0; i < stuff.Count; i++)