为什么我的MVC验证中ModelState.IsValid总是为true?
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;
}
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="tabDiv" style="margin: 0px 6px 6px 6px;">
<h2 style="margin: 5px 20px 5px 0px;">