I’m trying to generate a dropdownlist in my view, like the one below except I want my option label ("Please select...") to have a value of -1 as blank is already occupied by another option. Is there anyway to do this without adding it to the select list in my controller or view model, or creating a new html helper?
<%: Html.DropDownListFor(model => model.stuff.Id, new SelectList(Model.stuff, "Id", "Name"), "Please select...")%>
Thanks
You cannot do this out of the box with the DropDownListFor helper. It will always use blank for this value. You have two possibilities:
Actually you have a third possibility which IMHO is the best and what I would recommend you:
Don’t use blank as values. This should be reserved for the Please Select default value. It doesn’t seem logical to use blank as the value of something that the user can actually select. You might also have problems with the validation.