Friday, February 6, 2015

Adding Columns dynamically in Razor MVC

@{
    var gridColumns = new List<WebGridColumn>();
    gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Select", "Details", 
 new { contractId = item.ContractId })));
    if (Model.ShowEdit)
    {
        gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Edit", "Edit",  
new { contractId = item.ContractId })));
    }
    gridColumns.Add(grid.Column("SignOffDate", "Sign Off Date", 
format: @<text> <span>@item.SignOffDate.ToString("d/M/yyyy")</span></text>));
    gridColumns.Add(grid.Column("FullContractNumber", "Contract Number"));
    gridColumns.Add(grid.Column("ContractTitle", "Title"));
}

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

1 comment: