views:

189

answers:

3

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;} 
}
A: 

Why not give the empty option an explicit value of "0"?

EDIT: my bad, I missed that you were creating it with the helper

orip
I did end up adding 0 to the "select one" option by manually adding an object into the List that is used to build the SelectList. See my answer for details.
welzie
A: 

" NOT binding 0 to SpecialProjectId when it finds "" as the value for form field "

I think this is expected behavior.

Your implicitly telling the model binder to bind this SpecialProjectId to an int. It can't, therefore your model state is invalid.

You want to set your int to a nullable int?. Then your model will be valid.

Think of it as "can't bind" invalid rather than "validation fails" invalid.

jfar
I guess it is expected behavior. I just could have sworn that the default auto binder knew to turn "" into 0's for you. If I cared enough I would setup a simple asp.net mvc app without using the DataAnnotatons and test it further.
welzie
Switching my model fields to int? and other Nullable types helped me get the correct behavior. Only bad part is that I have to do a lot of blah.HasValue and blah.Value, but at least I don't see the "a value is required" messages anymore.
welzie
+1  A: 

This isn't an answer to why "" is not being turned into 0, but a way to avoid that issue in the first place.

//Manually add a "Select One" with value 0 to the SelectList

List<SpecialProject> specialProjectsForSelectBox = new List<SpecialProject>();
specialProjectsForSelectBox.Add(new SpecialProject(0, "* Select One *"));
specialProjectsForSelectBox.AddRange(specialProjectBusinessLogic.FindAllActive());
welzie
My hack of an answer works, but to avoid this hack simply switch your model objects to use Nullable types like int? and DateTime?. See my comment above for more details.http://msdn.microsoft.com/en-us/library/1t3y8s4s%28VS.85%29.aspx
welzie