【问题标题】:If condition or for loop in a webgridWebgrid 中的 if 条件或 for 循环
【发布时间】:2011-08-04 13:41:26
【问题描述】:

我在我的视图中使用这个 webgrid。

 <div class="grid">
 @{
var grid = new WebGrid(Model.SearchResults, canPage: true, rowsPerPage: 15);
grid.Pager(WebGridPagerModes.NextPrevious);
 @grid.GetHtml(
           htmlAttributes: new { @style = "width:100%", cellspacing = "0" },
           columns: grid.Columns(
           grid.Column(header: "Customer Name", format: (item) => Html.ActionLink((string)item.FullName, "ShowContracts", new { id = item.UserId }, new { @style = "color: 'black'", @onmouseover = "this.style.color='green'", @onmouseout = "this.style.color='black'" })),
           grid.Column(header: "SSN", format: item => item.SSN)
))
}
</div>

我使用 SSN 进行搜索并在 webgrid 中显示结果。显示的数据是虚拟数据。 我的视图模型中有一个 bool AccountVerified,现在我不应该为未验证的帐户提供操作链接,并在它们旁边显示说明帐户验证待定的文本。有人可以帮我吗?

【问题讨论】:

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


    【解决方案1】:

    尝试以下方法:

    grid.Column(
        header: "Customer Name", 
        format: (item) => 
            (bool)item.AccountVerified 
                ? Html.ActionLink(
                      (string)item.FullName, 
                      "ShowContracts", 
                      new { 
                          id = item.UserId 
                      }, 
                      new { 
                          style = "color: 'black'", 
                          onmouseover = "this.style.color='green'", 
                          onmouseout = "this.style.color='black'" 
                      }
                  ) 
                : Html.Raw("pending")
    )
    

    或编写一个自定义 HTML 帮助程序来避免这种怪物,并且简单地说:

    grid.Column(
        header: "Customer Name", 
        format: item => Html.PendingLink(item)
    )
    

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多