15

I have this:

@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"))

And would like it to be rendered as this:

<select required>
    <option>...</option>
    ...

How would I do this?

idlackage
  • 2,715
  • 8
  • 31
  • 52

3 Answers3

33

Use this:

@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"), new {required = "required"})

It won't achieve the shorthand <select required but it should have the same effect. Although I've found you could achieve that exact element using

@Html.DropDownListFor(x => x.SelectedValue, new SelectList(Model.SomeList, "Value", "Text"), new {required = (string)null})

Which is a little ugly.

maxshuty
  • 9,708
  • 13
  • 64
  • 77
Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142
0

VB.NET

@Html.DropDownListFor(Function(Model) Model.ProgramTypeId, New SelectList(Model.allPrograms, 
"ProgramTypeId", "ProgramName"), "Select Program....", 
New With {Key .class = "form-control", Key .required = "Required"})
JoshYates1980
  • 3,476
  • 2
  • 36
  • 57
0

Try the following

@Html.DropDownList("PlanType", null, "Select Plan Type", htmlAttributes: new { @class = "form-control", required = "Select Plan Type" })
ramrunner
  • 1,362
  • 10
  • 20