2016-01-09 77 views
-1

我有這種說法如何插入條件,如果在WebGridColumn

查看

@model IEnumerable<xxxx> 
@{ 
    var idGrid = "grid" + this.ViewBag.IdParameters ?? string.Empty; 
    var grid = new WebGrid(id: idGrid, rowsPerPage: this.RowsPerPage, ajaxUpdateContainerId: idGrid); 
    byte Status = Convert.ToByte(ViewData["StatusM"]); 

    var columns = new WebGridColumn[] { 
     grid.Column("Id", ReuniaoResources.Id, style: "center"), 
     grid.DescriptionColumn("Status", ReuniaoResources.Status, descriptor: this.GetDescriptor(), style: "center"), 

     if(Status == 4) 
     { 
     grid.ButtonColumn(x => x.Status == 0 ? "ico_edit.gif" : "ico_details.gif", 
      toolTip: ReuniaoResources.Management, 
      showRoles: ApplicationRoles.xxxxx, 
      urlFunction: x => x.Status == 0 ? Url.Action("xxxx", "xxxxx", new {id = x.Id}) : Url.Action("xxxx", "xxxxx", new {id = x.Id}) 
     ); 
     } 
     grid.ViewButtonColumn(enableRoles: ApplicationRoles.xxxx, toolTip: Geral.Visualizar), 
    }; 
} 
@grid.GetHtmlExtended(this.Model, this.RowsCount, page: this, columns: columns) 

插入時如果grid.ButtonColumn,顯示錯誤: enter image description here

我想這ButtonColumn似乎只滿足如果條件
我做錯了什麼?
如何使它工作?

+0

你不能有一個條件,如果在初始化網格。您的IF語句必須在初始化之外。 – Kami

回答

0

我希望下面的例子將幫助你

var gridColumns = new List<WebGridColumn>(); 
if (status == 4) 
{ 
    gridColumns.Add(companyGrid.Column(format: @<text><button class="edit-mode">Edit</button> </text>)); 
} 

gridColumns.Add(companyGrid.Column("ID", format: @<text>@item.CompanyID</text>)); 
gridColumns.Add(companyGrid.Column("ID", format: @<text>@item.CompanyName</text>)); 

@companyGrid.GetHtml(columns:companyGrid.Columns(gridColumns.ToArray()))