一旦我将自定义CSS应用于ValidationSummary和ValidationMessage,即使没有错误也会显示在页面上. CSS确实有一个图像.
@Html.ValidationSummary("",new { @class = "required" }) @Html.ValidationMessageFor(m => m.UserName,null,new { @class = "required" }) .required { background-image: url(/images/required.jpg); background-repeat: no-repeat; height: 33px; width: 386px; font-family: "Trebuchet MS",Arial,Helvetica,sans-serif; font-size: 13px; font-weight: bold; color: #1eb4ec; padding-top: 16px; padding-left: 54px; margin-left: 30px; }
解决方法
这些已经建立在css类型中:
/* Styles for validation helpers -----------------------------------------------------------*/ .field-validation-error { color: #ff0000; } .field-validation-valid { display: none; } .input-validation-error { border: 1px solid #ff0000; background-color: #ffeeee; } .validation-summary-errors { font-weight: bold; color: #ff0000; } .validation-summary-valid { display: none; }
在MVC3项目的默认css文件中.这些是根据ModelState是否具有指定的属性或ValidationSummary所有的任何错误而适当显示的.如果ModelState中没有错误,那么display:none;来自.field-validation-valid将隐藏错误.