[英]ASP.NET MVC ValidationMessage not showing for dropdownlist
I have set a Required
Validationmessage
for the Role
field in the ViewModel
.我已经为
ViewModel
的Role
字段设置了一个Required
Validationmessage
。 The dropdownlist
supposedly reflects the Role
field in the ViewModel
. dropdownlist
应该反映了ViewModel
的Role
字段。 However, when I did not select any values from the dropdownlist
, it does not show any error at all even though I already set the Required
validation for that field..any idea why?但是,当我没有从
dropdownlist
选择任何值时,即使我已经为该Required
设置了Required
验证,它也根本没有显示任何错误......知道为什么吗?
The 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; }
}
The View:风景:
<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>
You cannot get client side validation when you use an overload of DropDownList()
when the first parameter is IEnumerable<SelectListItem>
(ie when using the same name for the SelectList
as the property your binding to).当您使用过载你不能让客户端验证
DropDownList()
时,第一个参数是IEnumerable<SelectListItem>
即使用了相同的名称时SelectList
为您绑定的财产)。 If you inspect the html your generating, you will see that there are no data-val-*
attributes generated for the <select>
element.如果您检查生成的 html,您将看到没有为
<select>
元素生成的data-val-*
属性。
It would work using if you were to use (say)如果您要使用(例如),它会起作用
ViewBag.RoleList = new SelectList(...);
in the controller and在控制器和
@Html.DropDownList("Role", (IEnumerable<SelectListItem>),ViewBag.RoleList "Select user type", new { @class = "form-control" })
in the view, however since you have a view model (which is best practice), then add a property for the SelectList
to the model在视图中,但是由于您有一个视图模型(这是最佳实践),然后将
SelectList
的属性添加到模型中
public class RegisterViewModel
{
....
[Required(ErrorMessage = "Please select a user type")]
[Display(Name = "Select user type:")]
public string Role { get; set; }
public IEnumerable<SelectListItem> RoleList { get; set; }
}
and in the GET method, populate the collection before you pass the model to the view并在 GET 方法中,在将模型传递给视图之前填充集合
RegisterViewModel model = new RegisterViewModel()
{
RoleList = .... // your query to generate the SelectList
};
return View(model);
and in the view, use the strongly typed HtmlHelper
methods在视图中,使用强类型
HtmlHelper
方法
@Html.DropDownListFor(m => m.Role, Model.RoleList "Select user type", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Role)
Refer also this DotNetFiddle for further examples of why the property your binding to and the name of the ViewBag
for the SelectList
should not be the same.另请参阅此 DotNetFiddle以获取更多示例,说明为什么您绑定到的属性和
SelectList
的ViewBag
名称不应相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.