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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:31:14+00:00 2026-05-20T02:31:14+00:00

I have a guest book form created using asp.net mvc. The valid gender form

  • 0

I have a guest book form created using asp.net mvc.
The valid gender form field must be filled in by selecting a value from a drop down control. The drop down control has 3 options, i.e., “–Select–“, “Female”, “Male” and the “–Select–” is selected by default. The data model has been setup to force the visitor select either female or male but not “–Select–“.

We know that the visitor has a chance to temper the form data, so he can submit the gender form field pointing to a value that does not exist in the database.

My question is:

  1. Can DataAnnotation prevent the user from posting a form field that does not exist in a database?
  2. What is the preferred approach to counter this attempt? Do I have to check the submitted gender form field first before invoking SaveChanges()?
  • 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-20T02:31:15+00:00Added an answer on May 20, 2026 at 2:31 am

    It depends whether you need to provide the user with a specific error, or a clean validation message. In cases where the user is trying to tamper with the form post, I would not be too concerned about the user experience.

    If you care about this, you can use the IValidatableObject interface to perform a validation against the legal values:

    public class Person : IValidatableObject
    {
        public string Gender { get; set; }
    
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            //ValidGenders is a list of valid values, retrieved from the database
            if (!ValidGenders.Contains(Gender))
            { 
                yield return new ValidationResult("Gender is not valid", new string[] { "Gender" });
            }
        }
    }
    

    The Model validation performs validation using IValidatableObject just as it does using data annotation validation.

    On the other hand, if you don’t care about the user experience, you could let the error happen in the database, and handle the issue using your standard error handling pipeline. Assuming your foreign key constraints are in place, the operation will fail because the Gender value is not found in the Genders table, or whatever your setup may be.

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

Sidebar

Related Questions

I guess I have been using the Web Site model ever since .NET 2.0
Ok another WPF question, well I guess this is just general .NET. I have
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Please have a look at this form that I am trying to design with
I have inserted a hidden input in my comment form: $ip = $_SERVER['REMOTE_ADDR']; <input
I have a login page which has two options login as guest , login.
I have a login.jsp page which contains a login form. Once logged in the
I have a HashMap (although I guess this question applies to other collections) of

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.