ViewModel
のRole
フィールドにRequired
Validationmessage
を設定しました。 dropdownlist
はViewModel
のRole
フィールドを反映していると思われます。しかし、dropdownlist
から値を選択しなかった場合、そのフィールドにはすでにRequired
の検証が設定されていても、何のエラーも表示されません。理由は何ですか?ASP.NET MVC ValidationMessageがドロップダウンリストに表示されない
のViewModel:
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