samedi 9 mai 2015

Html.DropDownListFor selected value not being selected

I've got a drop down list where the selected calue is not being rendered correctly.

The code to create is as follows:

In the controller

        var dlvm = new DonorIndexViewModel();

        return View(dlvm);

The ViewModel is created by

    public DonorIndexViewModel() {
        var list = CreateSearchList();
        SearchList = new SelectList(list, "Value", "Text", list.First().Value);
    }

CreateSearchList is:

        private IEnumerable<SelectListItem> CreateSearchList() {
        var list = new List<SelectListItem> {
            CreateSelectListItem(Constants.SurnameName, Constants.SurnameValue),
            CreateSelectListItem(Constants.CodeName, Constants.CodeValue),
            CreateSelectListItem(Constants.PostcodeName, Constants.PostcodeValue),
            CreateSelectListItem(Constants.Address1Name, Constants.Address1Value)
        };


        return list;
    }

In the view, the model shows the first item is selected: enter image description here

But the item is not being selected in the browser. Rendered code is as follows:

<select class="control-label input-width-xlarge" data-val="true" data-val-      number="The field SearchListId must be a number." data-val-required="The SearchListId field is required." id="SearchListId" name="SearchListId">
<option value="">Select Search Type</option>
<option value="1">Surname</option>
<option value="2">Code</option>
<option value="3">PostCode</option>
<option value="4">Address (Line 1)</option>
</select>

Everything else works fine, e.g. selected value on postback is correct.

Thanks!

Aucun commentaire:

Enregistrer un commentaire