Matt Phillips
Matt Phillips

Reputation: 11519

asp.net helper for DropDownList won't render correctly

I'm attempting to get a dropdown list to select the correct value when the page loads.

@Html.DropDownList("CounterpartyTypeSelect", new SelectList(ViewBag.CounterpartyTypeOptions, "DefaultId", "Value"), new { @class = "selectbox", selected = Model.CounterpartyType }) 

However, when the page renders it always selects the first value in the dropdown list.

Screenshot of the page

The html source for the page:

    <select class="selectbox" id="CounterpartyTypeSelect" name="CounterpartyTypeSelect" selected="977980f2-ebb2-4c2a-92c2-4ecdc89b248d">
        <option value="5802239e-c601-4f1e-9067-26321213f6e6">Societa per Azioni (SpA)</option>
        <option value="f8160341-4a69-436f-9882-4da31a78f1d5">Gesellschaft mit beshrankter Haftung (GmbH)</option>
        <option value="977980f2-ebb2-4c2a-92c2-4ecdc89b248d">Sociedad Anonima (SA)</option>
        <option value="cdbeb1d3-301b-4884-b65a-612ddd8306f3">Private Limited Company (Ltd)</option>
        <option value="1fe68d96-f31b-4859-9869-8c76a5eb1508">Corporation (Inc)</option>
        <option value="9c9e5722-ab59-4d1c-a0a3-91b42a3ee721">Limitada (LTDA)</option>
        <option value="0cb57339-8705-4e3a-8f6a-95e9664962b7">Public Limited Company (Plc)</option>
        <option value="0924d6f1-06a9-49a3-ac05-b3e2686a0e92">Partnership</option>
        <option value="c8fbe021-a8f7-4e9d-ab38-dbeb5af5a631">Limited Liability Company (LLC)</option>
        <option value="30d9e22b-34f5-43c5-8471-e614dbedb6a6">Aktiengesellschaft(AG)</option>
    </select> 

As you can see it is putting the "selected" attribute into the outer select tag instead of on the option that matches the Id. I have another select with identical parameters (except variable names of course) and it renders correctly this way. I do not want to use DropDownListFor<T> because I have a HiddenFor field that actually submits this value in the form and javascript that sets the value to match the Select choice. I've confirmed my database is storing the values that I set correctly. What is going on?

Upvotes: 1

Views: 464

Answers (2)

arviman
arviman

Reputation: 5255

What's happening in the first line of your code new { @class = "selectbox", selected = Model.CounterpartyType } is you're providing the selected attribute as an HTML attribute of the parent select element.

So you see selected="977980f2-ebb2-4c2a-92c2-4ecdc89b248d" appearing in your first line of output, which btw doesn't do anything.

Also you're providing the value to search for in the Helper as a hardcoded string "value" instead of the actual value you need from the model. The browser will default to the first option since it can't find any value matching 'value'. To fix this, provide Model.CounterpartyType as the third parameter to your SelectList parameter instead of 'value'.

Upvotes: 0

ChevCast
ChevCast

Reputation: 59231

SelectList() has an overload for you to choose the selected item. You are adding it as an HTML attribute in the helper and those get rendered to the parent select tag.

Do this:

@Html.DropDownList("CounterpartyTypeSelect", new SelectList(ViewBag.CounterpartyTypeOptions, "DefaultId", "Value", Model.CounterpartyType), new { @class = "selectbox" })

Use the overload for SelectList that takes four arguments:

new SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue);

Upvotes: 2

Related Questions