如果是webgrid中的condition或for循环

时间:2011-08-04 13:41:26

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

我在我的视图中使用此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中显示结果。显示的数据是虚拟数据。 我在我的viewmodel中有一个bool AccountVerified,现在我不应该给未经验证的帐户提供操作链接,并在他们旁边显示文本说帐户验证待处理。有人可以帮我吗?

1 个答案:

答案 0 :(得分:3)

尝试以下方法:

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)
)