How to write IF condition in column of Web grid

谁都会走 提交于 2019-12-11 02:37:42

问题


i have get error the create the if condition in to column of WEB GRID so...please help to improve my code and solved to my prob....

grid.Column("Status", format: item => 
@<text>
@if (item.Is_active = true)
{ 
    @<a href="Url.Action("UserStatus", "Admin")"><img src="../../images/Active.png"/></a> 
} else
{ 
    @<a href="Url.Action("UserStatus", "Admin")"><img src="../../images/Deactive.png"/></a> 
}
</text>),

回答1:


The following should work:

grid.Column(
    "Status", 
    format: 
        @<a href="@Url.Action("UserStatus", "Admin")">
            <img src="@Url.Content(string.Format("~/images/{0}.png", item.Is_active ? "Active" : "Deactive"))" alt="" />
        </a>
)

Notice how I have fixed the src of your image because you have hardcoded it instead of using an url helper which you should never do.




回答2:


Try like this:

grid.Column("Status", format: item => 
        (bool)item.Is_active ?
        <a href="Url.Action("UserStatus", "Admin")"><img src="../../images/Active.png"/></a> :
        <a href="Url.Action("UserStatus", "Admin")"><img src="../../images/Deactive.png"/></a>)



回答3:


@{ WebGrid grid = new WebGrid(Model.LstContactUsers, selectionFieldName: "SelectedRow", canPage: false); @grid.GetHtml(tableStyle: "grid_rt", headerStyle: "background:none;height:0px;display:none", displayHeader: false, alternatingRowStyle: "grid-row-style", rowStyle: "grid-row-style", columns: grid.Columns( grid.Column(" ", header: null, style: "grid-row-style", format: @ @item.FName  @item.LName 
***@if( @item.Email.Contains("Group")) { @item.Email } else { 
@item.Email }*** )))
}


来源:https://stackoverflow.com/questions/14972848/how-to-write-if-condition-in-column-of-web-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!