user611684
user611684

Reputation: 35

DropDownList Validation - ModelState.IsValid is always false

I try to create a asp.net mvc 2 application. My DropDownList won't be validated!

I have a core model class called Animal with some attributes and the same for the class Genus. These classes are mapped to nHibernate.


namespace Core.Models
{
    public class Animal
    {
        public Animal() { }

        public virtual int AnimalId { get; set; }

        [DisplayName("Name")]
        [Required(AllowEmptyStrings = false, ErrorMessage = "Name is required")]
        public virtual string Name { get; set; }

        [DisplayName("Gattung")]
        [Required(ErrorMessage = "Genus is required")]
        public virtual Genus Genus { get; set; }
    }
}
namespace Core.Models
{
    public class Genus
    {
        public Genus() { }

        public virtual int GenusId { get; set; }

        [DisplayName("Name")]
        public virtual string GenusTitle { get; set; }
    }
}

In my UI Project I have a ViewModel class called AnimalViewModel


   public class AnimalViewModel
   {
        public Animal Animal { get; set; }
        public string ReturnUrl { get; set; }

        public IList Genus { get; set; }

        public AnimalViewModel(Animal a, string returnUrl)
        {
            this.Animal = a;
            this.ReturnUrl = returnUrl;            
        }

        public AnimalViewModel() { }
    }

Here's my view:


 model.Animal.Genus, new SelectList(Model.Genus, "GenusId", "GenusTitle"), "-- Fill out --")%>

And finally my Controller:


        public ActionResult Index(string returnUrl)
        {
            AnimalViewModel avm = new AnimalViewModel()
            {
                Animal = new Animal(),
                ReturnUrl = returnUrl,
                Genus = GenusRepository().GetAll()
            };
            return View(avm);
        }
        [HttpPost]
        public ActionResult Index(AnimalViewModel avm)
        {
            if (ModelState.IsValid) //is always false
            {
                //save
                return RedirectToAction("Overview");
            }
            else
            {
                Genus = GenusRepository().GetAll();
                return View(avm);
            }
        }

ModelState.IsValid is always false. I have no idea what's wrong. The client validation works but the server validation doesn't.

Any help would be appreciated.

thanks!

Upvotes: 0

Views: 6087

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You are not using the DropDownListFor helper properly. The first argument represents a property that will hold the selected value and must be of a scalar type. In your case you are passing a collection, the same one used as second argument. So It should be like this:

<%: Html.DropDownListFor(
    model => model.Animal.Genus.GenusId, 
    new SelectList(Model.Genus, "GenusId", "GenusTitle"), 
    "-- Fill out --"
)%>

Also what you are calling a AnimalViewModel is not a good naming convention because this is not a view model. Creating a class and stuffing all your models inside as public properties is a false idea of a view model. Here's how your view model might really look:

public class AnimalViewModel
{
    [DisplayName("Name")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Name is required")]
    public string AnimalName { get; set; }

    [DisplayName("Gattung")]
    [Required(ErrorMessage = "Genus is required")]    
    public int? SelectedGenusId { get; set; } 

    public IEnumerable<SelectListItem> Genus { get; set; }
}

Upvotes: 2

Related Questions