如果是Web Grid Column中的其他内容

时间:2013-12-24 10:01:09

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

如何在webgrid列中添加条件(if else)?

@grid.GetHtml(tableStyle: "table table-bordered",
                columns: grid.Columns(
                grid.Column("RealName", "Name"),
                grid.Column("UserName", "Email")
                ))

我必须根据条件显示电子邮件列,如何做到这一点?

5 个答案:

答案 0 :(得分:13)

你可以试试这个

@{
    var gridColumns = new List<WebGridColumn>();
    gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Select", "Details")));
    if (true)
    {
        gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Edit", "Edit")));
    }

    gridColumns.Add(grid.Column("UserName", "name"));
    gridColumns.Add(grid.Column("RealName", "RealName"));
}

@grid.GetHtml(columns: grid.Columns(gridColumns.ToArray()));

答案 1 :(得分:9)

这对我有用。

 @grid.GetHtml(tableStyle: "webGrid",
        headerStyle: "header",
        alternatingRowStyle: "alt",
        selectedRowStyle: "select",
        columns: grid.Columns(




        grid.Column("Is Active",format: (item) =>
            {
                if (item.IsActive == true)
                {
                    return Html.Raw(string.Format("<text><img height='20' width='20' src=\"{0}\" alt=\"Image\"/></text>", Url.Content("~/images/rightmark.png")));
                }
                else
                {
                    return Html.Raw(string.Format("<text><img height='20' width='20' src=\"{0}\" alt=\"Image\"/></text>", Url.Content("~/Content/images/non-preview-photo.gif")));                         
                }
            }, style: "firstColumn",canSort:true),       
        grid.Column("Name", " Name", style: "SecondColumn",canSort:true),       
        grid.Column("Role", "Role", style: "ThirdColumn",canSort:true)
))

答案 2 :(得分:3)

一种非常简单的方法是

if(myConditionCanGoInHere) {

   @grid.GetHtml(tableStyle: "table table-bordered",
            columns: grid.Columns(
            grid.Column("RealName", "Name"),
            grid.Column("UserName", "Email")
            ))

}
else{

 @grid.GetHtml(tableStyle: "table table-bordered",
            columns: grid.Columns(
            grid.Column("RealName", "Name"),
            //grid.Column("UserName", "Email")
            ))
// Here remove your email column

))

ReferenceHere

答案 3 :(得分:2)

我更新代码以接受参数:(Razor View - Webmatrix)

      grid.Column("Unread",format: (item) =>
        {
            if (item.Unread == true)
            {
                return Html.Raw(string.Format("<text><a \"target=\"_blank\" href=\"ViewComments?bvnum={0}\"><img height='20' width='20' border='0' src=\"/images/new_comments.png\" alt=\"Image\"/></text>", @item.id));
            }
            else
            {
                return Html.Raw(string.Format("<text><a \"target=\"_blank\" href=\"ViewComments?bvnum={0}\"><img height='20' width='20' border='0' src=\"/images/comments.png\" alt=\"Image\"/></text>", @item.id));
            }
        }, canSort:true)

答案 4 :(得分:1)

如果在将数据放入GUI层之前进行验证,那将是一件好事。 您必须在Controller中获取网格的正确数据。因此,您只能在网格中显示数据,并且您不必介意它是否是正确的数据,因为您已经对其进行了验证。

这意味着您必须将控制器中的if / else放在您的视图中。

        public JsonResult GetServiceGridData([DataSourceRequest]DataSourceRequest request)
        {
            var services = ModelTransformer.Transform(Repository.Instance.GetServices());
            foreach (var service in services)
            {
                var filterType = _filterTypes.FirstOrDefault(x => x.Id == service.FilterTypeId);
                service.FilterTypeName = filterType == null ? _filterTypeNoneName : filterType.Name;
            }
            return Json(services.ToDataSourceResult(request));
        }

例如

之类的东西