2

我将以下内容编码到我的 Razor 视图中:

        <ul class="message"
            data-elapsed="@Model.Events.Sum(event => event.Elapsed)"
            id="stats-list">
        @foreach (ViewEvent viewEvent in @Model.Events)
        {
            <li>@viewEvent.Description : @viewEvent.Elapsed ms</li>
        }
        <li class="close-bt"></li>
        </ul>

这给了我以下错误:

<div class="block-content no-title no-padding">
Line 63:             <ul class="message"
Line 64:                 data-elapsed=@(Model.Events.Sum(event => event.Elapsed)) 
Line 65:                 id="stats-list">
Line 66:             @foreach (ViewEvent viewEvent in @Model.Events)

但它给了我一个错误:

Compiler Error Message: CS1513: } expected

当我注释掉所有第 64 行时,我的代码显示了我的观点。

这不是获得总和的正确方法吗?

4

1 回答 1

4

尝试从 foreach 中删除第二个 @。它应该工作:

<ul class="message"
            data-elapsed="@Model.Events.Sum(event => event.Elapsed)"
            id="stats-list">
        @foreach (ViewEvent viewEvent in Model.Events)
        {
            <li>@viewEvent.Description : @viewEvent.Elapsed ms</li>
        }
        <li class="close-bt"></li>
        </ul>
于 2012-09-14T07:32:22.770 回答