My model object has an int property named SpecialProjectId. That property corresponds to a select box. I am using a default option in the select box. However when I submit the form with the default value the ModelState.isValid keeps being false because the default select option has value="" and for some reason the default binder is NOT binding 0 to SpecialProjectId when it finds "" as the value for form field SpecialProjectId. Below is all the code I could think to include. Please help. Note: I am using asp.net mvc 2.0.0 and the System.ComponentModel.DataAnnotations for validation.
//how I am creating the select box
<%= Html.DropDownList("SpecialProjectId", "* Select One *") %>
//what is created by the helper
<select id="SpecialProjectId" name="SpecialProjectId">
<option value="">* Select One *</option>
<option value="1">sp</option>
<option value="3">extra special project</option>
</select>
//my action method
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add([Bind(Exclude="Id, DateAudited")]AuditModel auditModel) {
try {
if (!ModelState.IsValid) {
return View("AuditForm", auditModel);
}
//...add audit code...
//my model object
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using Iesi.Collections;
public class AuditModel
{
public virtual int Id {get; set;}
//NOT REQUIRED!!!//
public virtual int SpecialProjectId {get; set;}
[Required]
public virtual int AccountId {get; set;}
[Required]
public virtual DateTime DateReceived {get; set;}
}