我已經爲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