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 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 am trying to get a guest book to work using PHP. I have
I'm a C# ASP.net MVC Developer. I got a new job and have to
I know this is a duplicate kind of question. I have worked with ASP.Net
So I have purchased the book Java for Dummies 4th Edition, and I must
I have created a zend form. I have added some elements. I have put
I currently have a guest book on my site. It is very basic. Recently
I have a table of recent web guest who have registered. One of the
I have a model called Contact which has_one guest like so. class Contact <
i have follow taxomony has been set up: accommodation -backpacker -guest -holiday and i
I have string like : '<New guest with special chars>' Its displaying nice with

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.