我已经为ViewModel
中的Role
字段设置了Required
Validationmessage
。据推测dropdownlist
反映ViewModel
中的Role
字段。但是,当我没有从dropdownlist
中选择任何值时,即使我已经为该字段设置Required
验证,它也不会显示任何错误。任何想法为什么?ASP.NET MVC ValidationMessage不显示下拉列表
视图模型:
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage = "Please select a user type")]
[Display(Name = "Select user type:")]
public string Role { get; set; }
}
的观点:
<div class="form-group">
@Html.LabelFor(m => m.Role, new { @class = "col-md-2 control-label"})
<div class="col-md-10">
@Html.DropDownList("Role", null, "Select user type", new { @class = "form-control" })
@Html.ValidationMessage("Role")
</div>
</div>
您的请求是否会触发控制器的操作,您是否可以检查模型的状态? – kat1330