开发者

Exception calling UpdateModel - Value cannot be null or empty

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!


Known issue with early previews of MVC 2. This was fixed for MVC 2 RTM. You can download MVC 2 RTM from http://www.asp.net/mvc/. Information on how to jury-rig this installation into VS2010 RC can be found at http://haacked.com/archive/2010/02/10/installing-asp-net-mvc-2-rc-2-on-visual-studio.aspx.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜