【问题标题】:Handle incrementation of value处理值的增加
【发布时间】:2013-05-26 07:59:48
【问题描述】:

我正在使用 ASP.Net MVC 并有以下代码。

@
{
   int id=0;
}
@foreach(var item in (<IEnumerable>Problem)ViewBag.Problems
{
     <div>
            <h2>  <a href="#" id=@id onclick="IncrementVisits(this)" > @item.Subject </a> </h2>
             @id++             -----Special Line 1
             <a href="#"> Visit(<span id="@id">@item.Visits</span>) </a> 
             <hr />
     </div>
        <br />
        id++;                   ------Specila Line 2   

}

问题是在特殊行 1 和 2(我给这些名称只是为了理解)我想增加 id 中的值,但不想在 div 中显示。如何避免 id 中的值不应显示在 div 中,并且还应该进行增量。请帮忙。

【问题讨论】:

  • 你知道预增量运算符吗?
  • 你试过@(id++)
  • 其实我认为显式表达式还是会输出视图中的值。尝试一个代码块。 @{id++}

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


【解决方案1】:

在这两种情况下,您都应该使用代码块:

@{
    id++;
}

与表达式(即@id++)相反,代码块不会产生任何默认输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-08
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多