2015-03-13 65 views
0

我想顯示鏈接基於如果在asp.net mvc中的網格條件的條件。 如果記錄超過結束日期,則不顯示鏈接。如何使用如果使用asp.net webgrid中的條件mvc

grid.Column("", format: (item => 
    { 
     if (DateTime.Now > item.EndDate) 
     { 
      Html.ActionLink(
       "File ClAIM", 
       "Edit", 
       new { id = item.id }, 
       new { @class = "action-link" }); 
     } 
     return false; 
    })) 

它在我的所有記錄中顯示爲false。任何幫助將不勝感激。

回答

2

您需要使用ternary operator方式如下:

grid.Column("", format: (item => { DateTime.Now > item.EndDate ? Html.ActionLink("File ClAIM", "Edit", new { id = item.id }, new { @class = "action-link" }) : String.Empty })) 
0

你缺少一個returnHtml.ActionLink(...)。由於return false不在else分支中,所以在任何情況下都會執行該分支。我在代碼片段中添加了一些格式。這點對我來說可能會更明顯。

順便說一下,false不是字符串。我對代碼編譯有些驚訝,但我不知道webgrid組件的細節。你也希望在else情況下返回一個字符串。 string.Emptynull" " - 無論你認爲合適。