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. 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.
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. Validation will treat this as required. Change your property to int?
or Nullable<int>
to make it not required.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.