Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6369263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:46:56+00:00 2026-05-25T00:46:56+00:00

I have a webgrid and there is a column I want to be visible

  • 0

I have a webgrid and there is a column I want to be visible only to certain users.
Currently I have coded the grid as follows

if (Context.User.IsInRole(Role.Inputter) || Context.User.IsInRole(Role.Administrator))
{
    @grid.GetHtml(columns: grid.Columns(
        grid.Column(format: (item) => Html.ActionLink("Select", "Details", new { contractId = item.ContractId })),
        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")
    ));
}
else
{ 
    @grid.GetHtml(columns: grid.Columns(
        grid.Column(format: (item) => Html.ActionLink("Select", "Details", 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")
    ));
}

But surely there is a better way without repeating all that code?
The only difference between the 2 column inputs is that I want to display the Edit link for particlaur users. So what is the best alternative way of doing that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:46:57+00:00Added an answer on May 25, 2026 at 12:46 am

    Try like this (untested, don’t have access to VS at the moment):

    @{
        var gridColumns = new List<WebGridColumn>();
        gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Select", "Details", new { contractId = item.ContractId })));
        if (Context.User.IsInRole(Role.Inputter) || Context.User.IsInRole(Role.Administrator))
        {
            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()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webgrid presenting data grouped by weeks. The first grid column contains
I want to explain this as best as I can. I have a Webgrid
I have an MVC3 webgrid where the first column is being hidden by jQuery
I Have a little problem with WebGrid control for Asp.Net MVC3. What I want
I have a ASP.Net MVC Webgrid and when the 'SuccessRate' column is less than
I have, surprisingly, been searching on the internet how to bind a webgrid column
I have a MVC3 WebGrid in my View.I want to display a tooltip when
I have a WebGrid bound to a Plain Class Object. Nothing Fancy. The Grid
I have the following WebGrid and just dont get why my column headings are
Have 2 tables in Access 2007, both lists of certain tasks to be accomplished.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.