[英]in .net mvc4 how to exclude dropdown from unobtrusive validation
I have optional dropdown and it triggers validation errors if selection not made, how do I make it optional but still bind it if selection is made. 我有可选的下拉菜单,如果未进行选择,它将触发验证错误,如何使它成为可选的,但如果进行选择,仍会绑定它。
got this answer link but it actually stops binding, that is not what I want, also I know how to get rid of unobtrusive validation completely and get it done classic way and then get what I want from http post but I want to know if mvc4 client validation is flexible for this. 得到了这个答案链接,但它实际上停止了绑定,这不是我想要的,我也知道如何完全摆脱不打扰的验证,并以经典方式完成它,然后从http发布中获得我想要的东西,但我想知道是否mvc4客户端验证对此非常灵活。 Basically I am validating server side and throwing exceptions then adding them to ModelState, so kind of need partial support from mvc built-in client validation and it works fine except for dropdowns. 基本上,我是在验证服务器端并抛出异常,然后将其添加到ModelState,因此需要mvc内置客户端验证的部分支持,并且除下拉列表外,它都可以正常工作。
my model has no "Required" attributes on properties and here is my form: 我的模型的属性上没有“必需”属性,这是我的表格:
@using (Html.BeginForm("Send", "Mail", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>New Support Ticket: </legend>
<ul>
<li>
<label for="@Model.TypeLookupId">Type</label>
@Html.DropDownListFor(m => m.TypeLookupId, Model.Types)
</li>
<li>
<label for="@Model.IssueName">Subject</label>
@Html.TextBoxFor(m => m.IssueName, Model.IssueName)
</li>
<li>
<label for="@Model.IssueNewNote">Comment</label>
@Html.TextAreaFor(m => m.IssueNewNote, string.Empty)
</li>
<li>
<input type="file" name="files" id="files" multiple="multiple"/>
</li>
<li>
<input type="submit" value="Save" />
</li>
</ul>
</fieldset>
}
int
are not nullable by default. int
默认情况下不可为空。 Validation will treat this as required. 验证将根据需要进行处理。 Change your property to int?
将您的属性更改为int?
or Nullable<int>
to make it not required. 或Nullable<int>
使其不需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.