I have two dropdowns created using HTML helpers, however i am not entirely sure on where i could add additional attributes to these two helpers? More specifically, i want to add an “onblur” attribute that calls a javascript function, as follows:
@onblur = "validate3(1)"
I want to be able to give the following HTML Helper dropdowns the above attribute:
@Html.DropDownList("ExpMonth", new List<SelectListItem>
{
new SelectListItem { Text = "01", Value = "1"},
new SelectListItem { Text = "02", Value = "2"},
new SelectListItem { Text = "03", Value = "3"},
new SelectListItem { Text = "04", Value = "4"},
new SelectListItem { Text = "05", Value = "5"},
new SelectListItem { Text = "06", Value = "6"},
new SelectListItem { Text = "07", Value = "7"},
new SelectListItem { Text = "08", Value = "8"},
new SelectListItem { Text = "09", Value = "9"},
new SelectListItem { Text = "10", Value = "10"},
new SelectListItem { Text = "11", Value = "11"},
new SelectListItem { Text = "12", Value = "12"}
}, "MM")
@Html.DropDownList("ExpYear", new List<SelectListItem>
{
new SelectListItem { Text = "2020", Value = "1"},
new SelectListItem { Text = "2021", Value = "2"},
new SelectListItem { Text = "2022", Value = "3"},
new SelectListItem { Text = "2023", Value = "4"},
new SelectListItem { Text = "2024", Value = "5"},
new SelectListItem { Text = "2025", Value = "6"},
new SelectListItem { Text = "2026", Value = "7"},
new SelectListItem { Text = "2027", Value = "8"},
new SelectListItem { Text = "2028", Value = "9"},
new SelectListItem { Text = "2029", Value = "10"},
new SelectListItem { Text = "2030", Value = "11"},
}, "YY")
Any help is appreciated.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Try
@Html.DropDownList("ExpMonth", new List<SelectListItem>{
new SelectListItem { Text = "01", Value = "1"},
new SelectListItem { Text = "02", Value = "2"},
new SelectListItem { Text = "03", Value = "3"},
new SelectListItem { Text = "04", Value = "4"},
new SelectListItem { Text = "05", Value = "5"},
new SelectListItem { Text = "06", Value = "6"},
new SelectListItem { Text = "07", Value = "7"},
new SelectListItem { Text = "08", Value = "8"},
new SelectListItem { Text = "09", Value = "9"},
new SelectListItem { Text = "10", Value = "10"},
new SelectListItem { Text = "11", Value = "11"},
new SelectListItem { Text = "12", Value = "12"}
}, "MM", new{@onblur = "validate3(1)"})
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0