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 5965629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:39:37+00:00 2026-05-22T19:39:37+00:00

We are using MVC3 with unobstructuve validation, all is fine; we got our custom

  • 0

We are using MVC3 with unobstructuve validation, all is fine; we got our custom validations working on the server as well as on the client.

We are also using the build-in asp.net Role provider. We have particular Views that we use for different Roles. Some of the fields (properties of the ViewModel) are Required based on the Role of the logged-in user.

My question is: How do we make a ValidationAttribute based on a Role? We want to make a property Required when the user is Normal User; but we don’t want to make that field required when the user is an Administrator. But we do want to use the same view.

I know we can create a custom ValidationAttribute (creating a class that derives from ValidationAttribute) I suppose that will work fine for Server-Side validation. But we really want to validate this Client-Side.

I reckon we implement the IClientValidatable in our above ValidationAttribute and write some jQuery to make an Ajax call back to the server to check the current logged in user and its roles.
But this will make us do an Ajax call every time the specific property changes, and the logged-in user will not change.

What is the right way to do this? Or should we create different ViewModels for different logged in user?

Please let me know when my above explanation doesn’t make sense, I can add some example code when needed.

Any help is appreciated.

Thanks,

Pim

  • 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-22T19:39:38+00:00Added an answer on May 22, 2026 at 7:39 pm

    I think you can indeed implement a custom ValidationAttribute that also implements the IClientValidatable. But then you could just “reuse” the unobtrusive required validation for the client side:

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
            {
                if ( <your condition, e.g. if user has a certain role> )
                    yield return new ModelClientValidationRequiredRule(<error message>);
            }
    

    This should generate the same unobtrusive required attributes on the input as the RequiredAttribute, but only if your condition is met.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a webpage built using MVC3 and .NET 4 Validation is working great
I'm using MVC3 with C# code. I have a table in my SQL Server
I'm using MVC3 webgrid to display, sort and paginate data. The display is fine,
I'm using MVC3 VS2010 with EF4.1, I have created my DB using SQL Server
I'm working on an ASP.NET MVC app (using MVC3 RC2). Say I have 2
Using MVC3 and Entity Framework. Am trying to get validation flowing from data model
Using MVC3.NET I have a file upload method in a controller that works fine
I am using MVC3 and added model validation with required attributes. Then I have
using MVC3, Code First (DbContext) Is there a way to implement (ideally) a facade
I'm using MVC3 with EF (version 4, but not sure if 4.0,4.1,etc). I've been

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.