2011-08-04 29 views
0

我在我的視圖中使用此webgrid。如果條件或for 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,現在我不應該給沒有驗證的帳戶提供操作鏈接,並在他們旁邊顯示文本說明帳戶驗證等待。有人可以幫助我嗎?

回答

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