DropDownListFor with an item value 0 always selected

混江龙づ霸主 提交于 2019-12-01 07:45:49

问题


I have a problem getting asp.net and the razor engine to create a drop down list with the correct element selected. No matter what I try, if one of the values available is "0", that item will always be selected.

What I am trying to create is a select for a rating that span from -2 to 2, with 0 being the middle value, but that should NOT be pre-selected. The user should be forced to make a decision and therefore the defualt value should be empty.

Creating the dropdownlist with an option label like the code below shows does not leave the default as empty. This would work if the values were 1-5 for instance.

@Html.DropDownListFor(model => model.Rating, new SelectList(new List<SelectListItem>()
{
    new SelectListItem() {Text = "-2", Value = "-2"},
    new SelectListItem() {Text = "-1", Value = "-1"},
    new SelectListItem() {Text = "0", Value = "0"},
    new SelectListItem() {Text = "1", Value = "1"},
    new SelectListItem() {Text = "2", Value = "2"}
}, "Value", "Text"), String.Empty, new { @class = "rating-select" })

The above code adds a selected tag to the 0-item and creates the following html:

<select class="rating-select valid" data-val="true" data-val-number="The field Rating must be a number." data-val-required="Rating is required." id="Rating" name="Rating">
    <option value=""></option>
    <option value="-2">-2</option>
    <option value="-1">-1</option>
    <option selected="selected" value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
</select>

I have tried all versions of selecting a blank value, but all result in the same; the 0-item gets generated with a selected tag.

Creating a blank item with Selected = true

@Html.DropDownListFor(model => model.Rating, new SelectList(new List<SelectListItem>()
{
    new SelectListItem() {Text = "Null", Value = "", Selected = True},
    new SelectListItem() {Text = "-2", Value = "-2"},
    new SelectListItem() {Text = "-1", Value = "-1"},
    new SelectListItem() {Text = "0", Value = "0"},
    new SelectListItem() {Text = "1", Value = "1"},
    new SelectListItem() {Text = "2", Value = "2"}
}, "Value", "Text"), String.Empty, new { @class = "rating-select" })

Creating a blank item object and setting the object as selected in selectlist

@{SelectListItem blankItem = new SelectListItem() {Text = "Null", Value = ""}}
@Html.DropDownListFor(model => model.Rating, new SelectList(new List<SelectListItem>()
{
    blankItem,
    new SelectListItem() {Text = "-2", Value = "-2"},
    new SelectListItem() {Text = "-1", Value = "-1"},
    new SelectListItem() {Text = "0", Value = "0"},
    new SelectListItem() {Text = "1", Value = "1"},
    new SelectListItem() {Text = "2", Value = "2"}
}, "Value", "Text", blankItem), String.Empty, new { @class = "rating-select" })

None of the above work when one of the values is 0.

Does anyone know of a proper solution or if this is a bug in the framework?

I can solve it with a work-around hack, but I'd rather not...


回答1:


I would look at what the type of the model.Rating property is, I am assuming it is an int. If this is an (int) type and you are not providing a value, it will be bound to a 0 when it comes across to a view. As such, when the view is processed, 0 will be marked as selected, that is what the view engine thinks the right value is and will override the selected value that you manually chose. Try making the model.Rating property a nullable int (int?) and see if you have the same issues.

Secondly, you could always look into a quick jQuery function to reset the drop-down to the first provided value, but that might be a little more "hacky" than what you are looking for.




回答2:


The solution is to either avoid an item having 0 as value because Rating is an int (with default value equal 0), or, handle binding of DropdownList yourself by using Html.DropDownList instead. If you choose latter, you also have to specify Selected for the item user has selected while binding it.



来源:https://stackoverflow.com/questions/18235229/dropdownlistfor-with-an-item-value-0-always-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!