Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding the 'required' attribute with DropDownListFor

Tags:

c#

razor

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?

like image 376
idlackage Avatar asked Sep 09 '13 18:09

idlackage


1 Answers

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.

like image 90
Yuriy Faktorovich Avatar answered Oct 06 '22 07:10

Yuriy Faktorovich