Exception calling UpdateModel - Value cannot be null or empty

Posted by James Alexander on Stack Overflow See other posts from Stack Overflow or by James Alexander
Published on 2010-04-08T14:30:38Z Indexed on 2010/04/08 14:33 UTC
Read the original article Hit count: 246

Filed under:
|

This is probably something silly I'm missing but I'm definitely lost. I'm using .NET 4 RC and VS 2010. This is also my first attempt to use UpdateModel in .NET 4, but every time I call it, I get an exception saying Value cannont be null or empty. I've got a simple ViewModel called LogOnModel:

[MetadataType(typeof(LogOnModelMD))]
public class LogOnModel
{
    public string Username { get; set; }
    public string Password { get; set; }

    public class LogOnModelMD
    {
        [StringLength(3), Required]
        public object Username { get; set; }
        [StringLength(3), Required]
        public object Password { get; set; }
    }
}

My view uses the new strongly typed helpers in MVC2 to generate a textbox for username and one for the password. When I look at FormCollection in my controller method, I see values for both coming through.

And last but not least, here's are post controller methods:

    // POST: /LogOn/
    [HttpPost]
    public ActionResult Index(FormCollection form)
    {
        var lm = new LogOnModel();

        UpdateModel(lm, form);

        var aservice = new AuthenticationService();

        if (!aservice.AuthenticateLocal(lm.Username, lm.Password))
        {
            ModelState.AddModelError("User", "The username or password submitted is invalid, please try again.");
            return View(lm);
        }

        return Redirect("~/Home");
    }

Can someone please lend some insight into why UpdateModel would be throwing this exception? Thanks!

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about .net-4.0