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
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:
Controller:
View: