简体   繁体   中英

how to use if condition in webgrid using asp.net mvc

I am trying to display the link based on if condition in web grid in asp.net mvc. If the record exceeds end date, then the link is displayed else not.

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

It is displaying false in all my records.Any help would be appreciated.

您需要通过以下方式使用三元运算符

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

You are missing a return before Html.ActionLink(...) . Since the return false is not in an else branch it is executed in either case. I added a bit of formatting to your code snippet. My point might be a bit more obvious to you this way.

By the way, false is not a string. I am slightly surprised that the code compiles, though I don't know the webgrid component in detail. You will want to return a string in the else case, too. string.Empty , null , " " - whatever you see fit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM