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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:06:43+00:00 2026-06-01T14:06:43+00:00

New to MVC, so hopefully this will be pretty simple. I have an HTML

  • 0

New to MVC, so hopefully this will be pretty simple. I have an HTML drop down list that I would like to set as required on a contact form, however when I apply data annotations do the class I can’t seem to get it to fire as required on the post back. The default value is “”

Here is my html snippet:

<div class="editor-field">
    <select name="ContactReason" size="1" class="textBox">
        <option value=""></option>
        <option value="I have a question about this website">I have a question about this website</option>
        <option value="My account is locked">My account is locked</option>
        <option value="I am experiencing problems with the website">I am experiencing problems with the website</option>
    </select>
    @Html.ValidationMessageFor(m => m.ContactReason)
</div>

Below is my C# code with my annotations. I have tried setting a required attribute and even a range attribute but neither seem to do the trick.

[Required(ErrorMessage = "Your reason for contacting is required.")]
[Range(2, 100,
ErrorMessage = "Your reason for contacting is required.")]
public virtual string ContactReason { get; set; }

Thank you for your guidance.

Flea

  • 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-01T14:06:44+00:00Added an answer on June 1, 2026 at 2:06 pm

    I found the following website helpful: http://codeoverload.wordpress.com/2011/05/22/dropdown-lists-in-mvc-3/

    I implemented the following and it worked well for me:

    Model:

    public List<SelectListItem> ContactReasons { get; set; }
    
    [Required(ErrorMessage = "Your reason for contacting is required.")]
    public virtual string ContactReason { get; set; }
    

    Controller:

    public ActionResult Index()
    {
        var Model = new ContactForm();
        Model.ContactReasons = this.GetContactReasons();
        return View(Model);
    }
    
    private List<SelectListItem> GetContactReasons()
    {
        List<SelectListItem> items = new List<SelectListItem>();
        items.Add(new SelectListItem { Text = "", Value = "" });
        items.Add(new SelectListItem { Text = "I have a question about this website", Value = "I have a question about this website" });
        items.Add(new SelectListItem { Text = "I am experiencing problems with the website", Value = "I am experiencing problems with the website" });
    
        return items;
    }
    

    View:

    <div class="editor-label">
        @Html.Label("I am contacting because:")
    </div><br />
    <div class="editor-field">
       @Html.DropDownListFor(m => m.ContactReason, Model.ContactReasons)
       @Html.ValidationMessageFor(m => m.ContactReason)
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question that has pretty much been asked here: asp.net mvc Html.ActionLink()
This will likely (hopefully?) be an amateur hour type question. :) I'm new to
I'm starting a new project which in simple terms will have a UI layer
I am new to spring MVC,and just started working on it.I would like to
I have started a new mvc project and clicked on project\properties\web - use local
I've just create a new MVC project, and have made no changes at all,
I can't find the Html.Image method in new MVC RC version. Please somebody give
I have various simple ASP.NET MVC views for CRUD operations which work fine on
I have a list of products coming from a repository. Simple enough. Now I
I have a new MVC application which integrates into a larger pre-existing intranet site.

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.