Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a boolean property editor to a dropdownlist in MVC view

I currently have scaffolded a view where a boolean property of my model gets passed to the Html.EditorFor helper:

@Html.EditorFor(model => model.EndCurrentDeal) 

All well and good, but what I'm really looking to do is massage that into a dropdown like:

<select>     <option value="true" selected="selected">Yes</option>     <option value="false">No</option> </select> 

What's the easiest way to achieve that?

Thanks,

Chris

like image 341
Mister Epic Avatar asked Mar 18 '13 14:03

Mister Epic


1 Answers

You can try something like here:

<%= Html.DropDownList(     "",      new SelectList(         new[]          {              new { Value = "true", Text = "Yes" },             new { Value = "false", Text = "No" },         },          "Value",          "Text",         Model     ) ) %> 

If you want a default Value :

<%= Html.DropDownList(         "",          new SelectList(             new[]              {                  new { Value = "", Text = "None" },                 new { Value = "true", Text = "Yes" },                 new { Value = "false", Text = "No" },             },              "Value",              "Text",             Model         )     ) %> 
like image 137
Francois Borgies Avatar answered Sep 24 '22 15:09

Francois Borgies