MVC 2 Data annotations problem

Posted by Louzzzzzz on Stack Overflow See other posts from Stack Overflow or by Louzzzzzz
Published on 2010-03-18T17:28:30Z Indexed on 2010/03/18 17:31 UTC
Read the original article Hit count: 447

Filed under:
|
|

Hi. Going mad now. I have a MVC solution that i've upgraded from MVC 1 to 2. It all works fine.... except the Validation!

Here's some code:

In the controller:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI;
using MF.Services.Authentication;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace MF.Controllers
{
     //basic viewmodel
     public class LogOnViewData
     {
     [Required]
     public string UserName { get; set; }

     [Required]
     public string Password { get; set; }
     }

    [HandleError]
public class AccountController : Controller
{
         [HttpPost]
         public ActionResult LogOn(LogOnViewData lvd, string returnUrl)
         {

         if (ModelState.IsValid)
         {
        //do stuff - IsValid is always true
         }
         }
    }
}

The ModelState is always valid. The model is being populated correctly however. Therefore, if I leave both username and password blank, and post the form the model state is still valid. Argh!

Extra info: using structure map for IoD. Previously, before upgrading to MVC 2 was using the MS data annotation library so had this in my global.asax.cs: ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();

Have removed that now.

I'm sure i'm doing something really basic and wrong. If someone could point it out that would be marvellous. Cheers

© Stack Overflow or respective owner

Related posts about mvc

Related posts about dataannotations