我正在开发一个 ASP.NET MVC3 应用程序,我无法让 DropDownListFor 在特定的编辑器视图中使用我的属性。
我的模型是一个Person
,一个人有一个属性,指定它是“人类型”。PersonType
是一个包含名称/描述和 ID 的类。我可以PersonType
通过名为ApplicationSettings
.
在我的编辑模板视图中,我Person
创建了一个SelectList
用于调试目的:
@ModelType MyNamespace.Person
@Code
Dim pTypesSelectList As New SelectList(MyNamespace.ApplicationSettings.PersonTypes, "ID", "Name", Model.PersonType.ID)
End Code
然后我将此SelectList
作为参数提供给DropDownListFor
绑定到PersonType
我的属性的Person
.
为了调试目的,我还打印了Selected
每个项目的属性:SelectList
<div style="text-align: center; margin: 5px 0 0 0;">
<div>
@Html.LabelFor(Function(model) model.PersonType)
</div>
<div>
@Html.DropDownListFor(Function(model) model.PersonType, pTypesSelectList)
@Html.ValidationMessageFor(Function(model) model.PersonType)
<br />
<br />
<!-- The following is debugging code that shows the actual value-->
@Model.Type.Name
<br />
@Model.Type.ID
<br />
<br />
<!--This section is to show that the select list has properly selected the value-->
@For Each pitem In pTypesSelectList
@<div>
@pitem.Text selected: @pitem.Selected
</div>
Next
</div>
</div>
该视图绑定到Person
其PersonType
属性为“Person Type # 2”的一个,我希望它被选中;但是此代码的 HTML 输出如下所示:
<div style="text-align: center; margin: 5px 0 0 0;">
<div>
<label for="PersonType">PersonType</label>
</div>
<div>
<select id="PersonType" name="PersonType">
<option value="7e750688-7e00-eeee-0000-007e7506887e">Default Person Type</option>
<option value="87e5f686-990e-5151-0151-65fa7506887e">Person Type # 1</option>
<option value="a7b91cb6-2048-4b5b-8b60-a1456ba4134a">Person Type # 2</option>
<option value="8a147405-8725-4b53-b4b8-3541c2391ca9">Person Type # 3</option>
</select>
<span class="field-validation-valid" data-valmsg-for="PersonType" data-valmsg-replace="true"></span>
<br />
<br />
<!-- The following is debugging code that shows the actual value-->
Person Type # 2
<br />
a7b91cb6-2048-4b5b-8b60-a1456ba4134a
<br />
<br />
<!--This section is to show that the select list has properly selected the value-->
<div>
Default Person Type selected: False
</div>
<div>
Person Type # 1 selected: False
</div>
<div>
Person Type # 2 selected: True
</div>
<div>
Person Type # 3 selected: False
</div>
</div>
</div>
如您所见,SelectList 中项目的打印 Selected 属性显示第三个项目是“Selected”。但让我抓狂的是,与此相对应的选项是未选择。