c# - 如何在我的 ASP.NET MVC 编辑页面中绑定(bind)多个下拉菜单?

标签 c# asp.net-mvc entity-framework html-helper

我有一个 View 模型,其中包含许多属性、一个 SelectList 和一个子对象数组。

public class RoundViewModel
{
    public int RoundId { get; set; }
    public string RoundName { get; set; }
    public SelectList Teams { get; private set; }
    public List<GameViewModel> Games { get; set; }
}

public class GameViewModel
{
    public int HomeTeamId { get; set; }
    public int AwayTeamId { get; set; }
    public DateTime GameTimeGMT { get; set; }
}

每轮游戏的数量是可变的,我正在尝试绑定(bind)相同的 Teams SelectList 到我放在页面上的每个下拉列表:

<% using (Html.BeginForm()) {%>

    <fieldset>
        <legend>Fields</legend>
        <%= Html.HiddenFor(model => model.CodeId) %>
        <div class="editor-label">
            <%= Html.LabelFor(model => model.RoundNumber) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.RoundNumber) %>
            <%= Html.ValidationMessageFor(model => model.RoundNumber) %>
        </div>

        <div class="editor-label">
            <%= Html.LabelFor(model => model.RoundName) %>
        </div>
        <div class="editor-field">
            <%= Html.TextBoxFor(model => model.RoundName) %>
            <%= Html.ValidationMessageFor(model => model.RoundName) %>
        </div>

        <%     
        for (int i = 0; i < Model.Games.Count; i++)
        {
        %>
        <div class="editor-label">
            Game:
        </div>
        <div class="editor-field">
        <%= Html.DropDownList("Games[" + i + "].HomeTeamId", Model.Teams, "--No Game--", new { value = Model.Games[i].HomeTeamId })%> VS 
        <%= Html.DropDownList("Games[" + i + "].AwayTeamId", Model.Teams, "--No Game--", new { value = Model.Games[i].AwayTeamId })%>

            <%= Html.TextBox("Games[" + i + "].GameTimeGMT", Model.Games[i].GameTimeGMT, new { Class = "calendar", value = Model.Games[i].GameTimeGMT })%>
        </div>
        <% } %>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>

类似的 View 适用于创建操作,但我似乎无法在每个下拉列表中设置现有选择。您可以看到我明确设置了每个游戏的日期时间。

new { value = Model.Games[i].HomeTeamId }行在 <select> 上设置一个属性html 元素,但显然这是行不通的。我想过设置另一个属性并使用 jQuery 设置选中项,但感觉比现有代码更 hacky。

我是 MVC 的新手,所以如果我以错误的方式进行操作,我将不胜感激。有人可以帮忙吗?

最佳答案

如果您查看 SelectList 对象的构造函数,您会发现它采用 IEnumerable 对象列表作为其源和其他参数来设置数据和文本字段以及所选项目。

您的 View 模型存储 SelectList 而不是团队列表有什么特别的原因吗?然后你就可以做

Html.DropDownList("Games[" + i + "].HomeTeamId", 
    new SelectList(Model.Teams, Model.Games[i].HomeTeamId));

public class RoundViewModel
{
    ....
    public IList<TeamObject> Teams { get; private set; }

关于c# - 如何在我的 ASP.NET MVC 编辑页面中绑定(bind)多个下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2356178/

相关文章:

c# - 如何使用 C# 访问特定的 XML 元素

c# - 在 C# 控制台应用程序上更改背景颜色

c# - 在(通过转换为状态机)递归生成器方法中摆脱 'yield' 的最简单方法?

asp.net-mvc - asp.net mvc 3,主题(不同 View )

css - asp.net mvc - 无法在 li 标签内设计子类

c# - Newtonsoft.Json.Linq.JObject.ToObject() 以字符串格式转换日期

asp.net - 在 View 中显示经过身份验证和匿名的内容

entity-framework - 如何将一个自定义实体映射到 Entity Framework 中的某些数据库表?

jquery - ASP.NET MVC : How to 'model bind' a non html-helper element

c# - 无法使用 1 : 1 mapping using EF Code First 将记录保存到数据库