Note that there are some explanatory texts on larger screens.

plurals
  1. PODropDownListFor not selecting the selected item in the SelectList
    primarykey
    data
    text
    <p>I am working on an ASP.NET MVC3 application and I cannot get a DropDownListFor to work with my property in a particular editor view.</p> <p>My model is a <code>Person</code> and a person has a property that specifies it's "Person Type". <code>PersonType</code> is a class that contains a name/description and an ID. I can access the available <code>PersonType</code>s within the application through my static/shared class called <code>ApplicationSettings</code>.</p> <p>In the Edit Template view for my <code>Person</code> I have created a <code>SelectList</code> for debugging purposes:</p> <pre><code>@ModelType MyNamespace.Person @Code Dim pTypesSelectList As New SelectList(MyNamespace.ApplicationSettings.PersonTypes, "ID", "Name", Model.PersonType.ID) End Code </code></pre> <p>I am then providing this <code>SelectList</code> as a parameter to the <code>DropDownListFor</code> that is bound to the <code>PersonType</code> property of my <code>Person</code>. </p> <p>I am also printing the <code>Selected</code> property of each item in the <code>SelectList</code> for debugging purposes:</p> <pre><code>&lt;div style="text-align: center; margin: 5px 0 0 0;"&gt; &lt;div&gt; @Html.LabelFor(Function(model) model.PersonType) &lt;/div&gt; &lt;div&gt; @Html.DropDownListFor(Function(model) model.PersonType, pTypesSelectList) @Html.ValidationMessageFor(Function(model) model.PersonType) &lt;br /&gt; &lt;br /&gt; &lt;!-- The following is debugging code that shows the actual value--&gt; @Model.Type.Name &lt;br /&gt; @Model.Type.ID &lt;br /&gt; &lt;br /&gt; &lt;!--This section is to show that the select list has properly selected the value--&gt; @For Each pitem In pTypesSelectList @&lt;div&gt; @pitem.Text selected: @pitem.Selected &lt;/div&gt; Next &lt;/div&gt; &lt;/div&gt; </code></pre> <p>The view is bound to a <code>Person</code> whose <code>PersonType</code> property is "Person Type # 2" and I expect this to be selected; however the HTML output of this code looks like this:</p> <pre><code>&lt;div style="text-align: center; margin: 5px 0 0 0;"&gt; &lt;div&gt; &lt;label for="PersonType"&gt;PersonType&lt;/label&gt; &lt;/div&gt; &lt;div&gt; &lt;select id="PersonType" name="PersonType"&gt; &lt;option value="7e750688-7e00-eeee-0000-007e7506887e"&gt;Default Person Type&lt;/option&gt; &lt;option value="87e5f686-990e-5151-0151-65fa7506887e"&gt;Person Type # 1&lt;/option&gt; &lt;option value="a7b91cb6-2048-4b5b-8b60-a1456ba4134a"&gt;Person Type # 2&lt;/option&gt; &lt;option value="8a147405-8725-4b53-b4b8-3541c2391ca9"&gt;Person Type # 3&lt;/option&gt; &lt;/select&gt; &lt;span class="field-validation-valid" data-valmsg-for="PersonType" data-valmsg-replace="true"&gt;&lt;/span&gt; &lt;br /&gt; &lt;br /&gt; &lt;!-- The following is debugging code that shows the actual value--&gt; Person Type # 2 &lt;br /&gt; a7b91cb6-2048-4b5b-8b60-a1456ba4134a &lt;br /&gt; &lt;br /&gt; &lt;!--This section is to show that the select list has properly selected the value--&gt; &lt;div&gt; Default Person Type selected: False &lt;/div&gt; &lt;div&gt; Person Type # 1 selected: False &lt;/div&gt; &lt;div&gt; Person Type # 2 selected: True &lt;/div&gt; &lt;div&gt; Person Type # 3 selected: False &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>As you can see the printed Selected properties for the items in the SelectList shows that the 3rd item is "Selected". But what is driving me crazy is that the option that corresponds with this is Not Selected.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload