DropDownList focuses when Submitting Modal - MVC

49 views Asked by At

In my razor view, I have a Drop down list

@Html.DropDownListFor(model => model.IntentionID, ViewBag.Intentions as IEnumerable<SelectListItem>, "", htmlAttributes: new { @class = "form-control minimal")

This is within a form, within a partial view displayed in a modal. This is not a required field, so it can be left alone and in some use cases will need to be left alone. But, if I haven't touched it and go to submit, the focus goes to the DropDown and I am unable to submit.

What shall I do to get around this? Or, avoid this from happening?

// EDIT: I have found that adding a SelectListItem to the start of the list and setting it as 'Selected' as satisfied it and stopped this issue.

This is not an ideal solution, however.

1

There are 1 answers

0
MisterHow On

I have found an answer that satisfies me. I wanted the DropDownListFor helper to accept no entries. By making the ID, that was being set by the DropDown, nullable it allowed the DropDown to move on. This does cause more checks and casts later down the line, but this maintainable.