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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:50:14+00:00 2026-06-01T10:50:14+00:00

I am using javascript unobtrusive validation. I have a view model that I am

  • 0

I am using javascript unobtrusive validation. I have a view model that I am re-using for several forms, and in some of the forms, some properties on the view model are required. On other forms they shouldn’t be. Is there a way to programmatically set [Required] on properties, so that I can accomplish this?

Thanks!

  • 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-01T10:50:15+00:00Added an answer on June 1, 2026 at 10:50 am

    Cannot be done using DataAnnotations as these are implemented at compile time and cannot be added dynamically. You can either

    Create different View Models that have the proper annotations

    or

    Have a service that you send the view model to that checks the model based on the action that it is coming from and return a list of validation errors that you can append to your model state

    or

    Put a property on the ViewModel such as string IsBeingUsedFor and use that in combination with a RequiredIf DataAnnotation. Here is an example of a library already build that uses conditional DataAnnotations. Then you can say, [RequireIf(“IsBeingUsedFor”, “Action_A”)]

    These aren’t necessarily all of the options, but the some of the cleaner ones. You could do this all in JavaScript, but you will lose server side validation and could open up some holes in your application if a ‘bad person’ submits the form and bypasses client side validation.

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

Sidebar

Related Questions

I'm using MVC3 with unobtrusive validation. I have a field that the user is
I'm using asp.net mvc 3 with unobtrusive jquery validation. I have a form that
I'm using MVC 3 with unobtrusive javascript for client validation. I have a table
We have a ASP.NET MVC 3 application that uses unobtrusive jQuery validation. The page
Im using the built in jQuery validation <script src=@Url.Content(~/Scripts/jquery.validate.min.js) type=text/javascript></script> <script src=@Url.Content(~/Scripts/jquery.validate.unobtrusive.min.js) type=text/javascript></script> Works
I have a very odd problem with unobtrusive validation. I have a model with
I have the following view in ASP.net MVC 3: @model Models.CreateProjectViewModel <script type=text/javascript src=@Url.Content(~/Scripts/jquery.validate.min.js)></script>
I am using Asp.Net Mvc3 and the unobtrusive jquery validation. I'd like to have
I have the following code in a partial view: @model MyOrganization.MyApp.Models.ProductListing @using (Ajax.BeginForm(TagProduct, new
I'm looking for a good 10 minute introduction to Unobtrusive Javascript using JQuery. I'm

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.