1

Possible Duplicate:
MVC 3 Webgrid - how do you hide columns you do not want to be visible?

I am using a WebGrid in my MVC application. What I want to do is put an if statement inside my form to hide a column depending on the condition. The code below shows what I mean with the if statement, but this is not allowed;

@grid.GetHtml(columns: grid.Columns(
    grid.Column(format: (item) => Html.ActionLink("Select", "Details", new { contractId = item.ContractId })),
    if(Context.User.IsInRole(ITOF.Web.Models.Role.Inputter)
    {
        grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { contractId = item.ContractId })),
    }
    grid.Column("SignOffDate", "Sign Off Date",
        format:@<text> <span>@item.SignOffDate.ToString("d/M/yyyy")</span></text>),
    grid.Column("FullContractNumber", "Contract Number"),
    grid.Column("ContractTitle", "Title")
));
Community
  • 1
  • 1
arame3333
  • 9,887
  • 26
  • 122
  • 205

1 Answers1

0

I don't know if this works, because I don't know the inner workings of the helper. You probably could do something like this:

    @{
        var temp = grid.GetHtml(....);        

        if(Context.User.IsInRole(ITOF.Web.Models.Role.Inputter)
        {
            temp.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { contractId = item.ContractId })),
        }
    }

    @Html.Raw(temp);

The helper should return an grid object not a string, otherwise you can't add the columns anymore.

Robin van der Knaap
  • 4,060
  • 2
  • 33
  • 48