Choose which option is selected on pageload of razor Html.DropDownListFor() in HTML?


Shorthand is used to decide which option is selected on pageload of razor Html.DropDownListFor(). You can try to run the following code snippet −

// return Boolean value
@Html.DropDownListFor(m => m.Valeur,
new List< SelectListItem > { //new list item list item1 is generated
new SelectListItem { Value = "0" , Text = "Show", Selected = Model.Valeur == 0 },
new SelectListItem { Value = "1" , Text = "Hide", Selected = Model.Valeur != 0 },
}, new { @class = “selectnew" })

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 04-Mar-2020

83 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements