为什么下拉列表中没有选择任何选项?
我很困惑为什么没有选择选择列表中的相应选项。 当我单步执行循环时,item.DisciplineId = 2
,但未选择 School(“选择”仍然是)。有什么建议吗?
视图
<% foreach (var item in Model.TeamMembers)
{ %>
<tr>
<td>
<%: Html.DropDownListFor(m => item.DisciplineId, Model.MemberDisciplines, "Choose") %>
</td>
</tr>
<% } %>
视图模型
public SelectList MemberDisciplines { get; set; }
public IEnumerable<TeamMember> TeamMembers { get; set; }//Set from the model
MemberDisciplines = new SelectList(new[] {
new SelectListItem{ Text = "Technical", Value = "1"},
new SelectListItem{ Text = "School", Value = "2"},
new SelectListItem{ Text = "Health", Value = "3"}
}, "Value", "Text");
I'm confused as to why the corresponding option in the select list is not being selected.
When I step through the loop, item.DisciplineId = 2
, but School is not selected ("Choose" still is). Any suggestions ?
View
<% foreach (var item in Model.TeamMembers)
{ %>
<tr>
<td>
<%: Html.DropDownListFor(m => item.DisciplineId, Model.MemberDisciplines, "Choose") %>
</td>
</tr>
<% } %>
View Model
public SelectList MemberDisciplines { get; set; }
public IEnumerable<TeamMember> TeamMembers { get; set; }//Set from the model
MemberDisciplines = new SelectList(new[] {
new SelectListItem{ Text = "Technical", Value = "1"},
new SelectListItem{ Text = "School", Value = "2"},
new SelectListItem{ Text = "Health", Value = "3"}
}, "Value", "Text");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我建议您将 MemberDisciplines 属性转换为具有以下内容的方法:
然后您在 DropDownListFor 中调用它,如下所示:
Alright, I would then suggest you to convert MemberDisciplines property into a method with the following content:
And then you call it in your DropDownListFor like so: