[英]ASP.Net MVC DropDown validation error even though not enabled
I have a property called Campus2Id that being rendered as a dropdown using the built-in HtmlHelper in Razor. 我有一个名为Campus2Id的属性,可以使用Razor中的内置HtmlHelper呈现为下拉列表。 By default, unobstrusive and jquery validate is enabled.
默认情况下,启用unobstrusive和jquery验证。
When I do a form submit, somehow the ModelState shows that there is an error for that property; 当我提交表单时,ModelState会以某种方式显示该属性存在错误; however, I did not add any validation attribute like Required to that property.
但是,我没有向该属性添加任何必需的验证属性。
Is there a reason why Mvc marks this property having a validation error? Mvc是否将此原因标记为此属性具有验证错误?
Model 模型
[Display(Name = "Campus")]
public Int32 Campus2Id { get; set; }
View 视图
<div class="form-group">
@Html.LabelFor(model => model.Campus2Id, new { @class = "sr-only" })
@Html.DropDownListFor(model => model.Campus2Id, new SelectList(Model.Campuses, "Id", "ShortName"), Model.CampusesLabel, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Campus2Id)
</div>
The error seems to be that the required validation is getting triggered on the Campus2Id field even though I did not specify [Required] attribute. 错误似乎是即使我未指定[Required]属性,所需的验证也会在Campus2Id字段上触发。
I would suggest making the Campus2Id nullable. 我建议使Campus2Id可为空。
That should resolve the validation error. 那应该解决验证错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.