如何在Razor MVC4视图中总结值?

时间:2012-09-14 07:14:02

标签: asp.net-mvc asp.net-mvc-3 razor

我正在将以下内容编入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行时,我的代码显示了我的观点。

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

1 个答案:

答案 0 :(得分: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>