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

  • SEARCH
  • Home
  • 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 8490841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:14:36+00:00 2026-06-10T22:14:36+00:00

I am generating a lot of views using data annotations and a few custom

  • 0

I am generating a lot of views using data annotations and a few custom templates.

public class Container
{
    [HiddenInput(DisplayValue = false)]
    public string Key { get; set; }

    [Display(Name = "Full Name")]
    [RequiredForRole("Editor"), StringLength(30)]
    public string Name { get; set; }

    [Display(Name = "Short Name")]
    [RequiredForRole("Editor"), StringLength(10)]      
    public string ShortName { get; set; }

    [Display(Name="Maximum Elements Allowed")]
    [RequiredForRole("Admin")]
    public int MaxSize { get; set; }

    [Display(Name = "Components")]
    public IList<Component> Components{ get; set; }
}

In the views, I just use @Html.DisplayForModel(), @Html.EditorForModel, etc.

Certain properties need to be editable by users in some roles but hidden for others. As you can see, I’ve implemented a custom validation attribute RequiredForRole which checks a value exists but only if the current user has a certain role.

I really need a custom Display attribute, but as DisplayAttribute is sealed, this doesn’t seem possible.

I want to avoid having lots of different templates for different kinds of users, or start pushing this logic of who sees what down onto the views. What’s the neatest way to solve this problem?

  • 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-06-10T22:14:38+00:00Added an answer on June 10, 2026 at 10:14 pm

    Something like this, maybe. The (BIG) problem is : how to check the current user’s role…

    public class VisibleForRoleAttribute : Attribute, IMetadataAware
        {
            public string[]  Roles { get; set; }
            public VisibleForUserAttribute(string[] roles)
            {
                Roles = roles;
            }
            public void OnMetadataCreated(ModelMetadata metadata)
            {
                var toShow =  Roles.Any(IsUserInRole);
                metadata.ShowForDisplay = metadata.ShowForEdit =  toShow; // or just ShowForEdit
    
            }
            private bool IsUserInRole(string roleName)
            {
                return HttpContext.Current != null &&
                       HttpContext.Current.User != null &&
                       HttpContext.Current.User.IsInRole(roleName); //if you use MembershipProvider
            }
        }
    

    usage

    [VisibleForRole(new[]{"Administrator", "Editor"})]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm seeing a lot of people using the Razor view engine for email templates.
For one of my data analysis pipelines, I end up generating a lot of
i am generating following html in my web page using jquery. <input id=left_div_foretagname class=userinfofont
We're running continuous integration builds from TFS, and generating a lot of symbol files
A lot of web frameworks have a standard setup for generating forms with auth
Generating normal columnar data in excel file is quite easy but does any one
I'm generating json from a Rails app, and am filtering fields using the format.json
I seem to be generating a lot of questions with this one little iPhone
as title, I have having quite a lot of panels when generating a chart
I am generating all dates between two dates. public List<Date> generateRangeDates(Date firstDate, Date lastDate)

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.