为什么我的MVC验证中ModelState.IsValid总是为true?
publicclassHomeController:Controller { ...
public class HomeController : Controller { public ActionResult Create() { return View("Create"); } [HttpPost] [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Role role) { if (ModelState.IsValid) { return this.View("RoleList"); } return this.View(role); } } public partial class Role { public bool IsValid { get { return (GetRuleViolations().Count() == 0); } } public IEnumerable<RuleViolation> GetRuleViolations() { if (String.IsNullOrEmpty(RoleID)) yield return new RuleViolation("RoleID不能为空", "RoleID"); yield break; } } public class RuleViolation { public string ErrorMessage { get; private set; } public string PropertyName { get; private set; } public RuleViolation(string errorMessage, string propertyName) { ErrorMessage = errorMessage; PropertyName = propertyName; } }
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询