28

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

Mister Epic
  • 16,295
  • 13
  • 76
  • 147

4 Answers4

29

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
        )
    ) %>
Community
  • 1
  • 1
Francois Borgies
  • 2,378
  • 31
  • 38
24

MVC 4

@*/////////////////// bool ////////////////////////////////*@
@model bool

@Html.DropDownListFor(m => m, new SelectList(
        new[] 
        { 
            new { Value = "true", Text = "Yes" },
            new { Value = "false", Text = "No" },
        },
        "Value",
        "Text",
        Model
    ))

@*/////////////////// bool? ////////////////////////////////*@    
@model bool?

@Html.DropDownListFor(m => m, new SelectList(
        new[] 
        { 
            new { Value = "", Text = "(none)" },
            new { Value = "true", Text = "Yes" },
            new { Value = "false", Text = "No" },
        },
        "Value",
        "Text",
        Model
    ))
serge
  • 13,940
  • 35
  • 121
  • 205
10

You can also try Dictionary approach is a bit cleaner and can be easily added to your constructor for View Model.

ViewModel

 //Define IDictionary interface
 public IDictionary<bool, string> options { get; set; }

public YourViewModel()
{
  // Default constructor
        this.options = new Dictionary<bool, string>();
        this.options.Add(false, "no");
        this.options.Add(true, "yes");
}




@Html.DropDownListFor(model => model.yourPropertyToEdit, new SelectList( Model.options, "Key", "Value"))
Adam Bielecki
  • 657
  • 2
  • 6
  • 20
5

I got inspired with the very useful answer from Francois Borgies, so I decided to write a custom method that creates SelectList for Boolean value that can be used in @Html.DropDownList. When you have a helper method that can be used in every view, then it reduces the amount of code needed in razor views.

My project has CustomHelpers.cs class in folder: App_Code/Helpers

namespace YourProjectName.App_Code.Helpers
{
    public static class CustomHelpers
    {
        public static SelectList SelectListForBoolean(object selectedValue = null)
        {
            SelectListItem[] selectListItems = new SelectListItem[2];

            var itemTrue = new SelectListItem();
            itemTrue.Value = "true";
            itemTrue.Text = "Yes";
            selectListItems[0] = itemTrue;

            var itemFalse = new SelectListItem();
            itemFalse.Value = "false";
            itemFalse.Text = "No";
            selectListItems[1] = itemFalse;

            var selectList = new SelectList(selectListItems, "Value","Text", selectedValue);

            return selectList;
        }           
    }
}

In the create view you can use it as follows:

@model Foo
@using YourProjectName.App_Code.Helpers;
...
@Html.DropDownList("EndCurrentDeal", CustomHelpers.SelectListForBoolean(), "-select-")

for edit view

@model Bar
@using YourProjectName.App_Code.Helpers;
...
@Html.DropDownList("EndCurrentDeal", CustomHelpers.SelectListForBoolean(Model.EndCurrentDeal), "-select-")

Although my helper method is not pure HTML Helper because it creates SelectList, - it nevertheless follows same functionality that was presented by Rahul Rajat Singh, in his excellent article An Absolute Beginner's Tutorial on HTML Helpers and Creating Custom HTML Helpers in ASP.NET MVC

jyrkim
  • 2,849
  • 1
  • 24
  • 33