为什么下拉列表中没有选择任何选项?

发布于 2024-10-06 16:50:39 字数 854 浏览 1 评论 0原文

我很困惑为什么没有选择选择列表中的相应选项。 当我单步执行循环时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

活雷疯 2024-10-13 16:50:39

好吧,我建议您将 MemberDisciplines 属性转换为具有以下内容的方法:

public SelectList GetMemberDisciplines(object selectedValue)
{
    return new SelectList(new[] {
        new SelectListItem{ Text = "Technical", Value = "1"},
        new SelectListItem{ Text = "School", Value = "2"},
        new SelectListItem{ Text = "Health", Value = "3"}
    }, "Value", "Text", selectedValue);
}

然后您在 DropDownListFor 中调用它,如下所示:

<% for (int i = 0; i < Model.TeamMembers.Count; i++) { %>
  <tr>
      <td>
          <%: Html.DropDownListFor(m => Model.TeamMembers[i].DisciplineId, Model.GetMemberDisciplines(Model.TeamMembers[i].DisciplineId), "Choose..")%>
      </td>
  </tr>
<% } %>

Alright, I would then suggest you to convert MemberDisciplines property into a method with the following content:

public SelectList GetMemberDisciplines(object selectedValue)
{
    return new SelectList(new[] {
        new SelectListItem{ Text = "Technical", Value = "1"},
        new SelectListItem{ Text = "School", Value = "2"},
        new SelectListItem{ Text = "Health", Value = "3"}
    }, "Value", "Text", selectedValue);
}

And then you call it in your DropDownListFor like so:

<% for (int i = 0; i < Model.TeamMembers.Count; i++) { %>
  <tr>
      <td>
          <%: Html.DropDownListFor(m => Model.TeamMembers[i].DisciplineId, Model.GetMemberDisciplines(Model.TeamMembers[i].DisciplineId), "Choose..")%>
      </td>
  </tr>
<% } %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文