现在要疯了。我有一个 MVC 解决方案,我已经从 MVC 1 升级到 2。一切正常......除了验证!
这是一些代码:
在控制器中:
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
}
}
}
}
ModelState 始终有效。但是,该模型已正确填充。因此,如果我将用户名和密码都留空,并发布表单,模型状态仍然有效。啊!
额外信息:使用 IoD 的结构图。以前,在升级到 MVC 2 之前使用 MS 数据注释库,所以在我的 global.asax.cs 中有这个: ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();
现在已经删除了。
我确定我在做一些非常基本和错误的事情。如果有人能指出这将是了不起的。干杯