Search code examples
asp.net-mvcrazorhtml-helper

@Html.DropDownListFor How to add option?


@Html.DropDownListFor(model => model.ZipFile, new SelectList(ViewBag.ZipFiles))

The above code creates me a select list just fine. But I want to make the selection optional. Unfortunately there is no empty option and I'd like to add one in. How would I do this?


Solution

  • By using the proper DropDownListFor overload:

    @Html.DropDownListFor(
        model => model.ZipFile, 
        new SelectList(ViewBag.ZipFiles),
        "-- please select a zip file --"
    )