在ASP.NET MVC中,DropDownList未正确填充值

5

我遇到了一个问题,无法在下拉列表中选择正确的值。它总是默认为索引零。但是,在另一个地方,我有一个视图,渲染得很好,并选择了正确的值。我不明白其中的区别。

这是一个精简版本的视图,可以正常工作:

@model AppName.Models.Guest

@using (Html.BeginForm())
{
    Attending Ceremony?<br />
    @Html.DropDownListFor(x => x.ConfirmCeremony, new SelectList(new Dictionary<string, string>
    {
        {"No Answer", "No Answer"},
        {"Yes", "Yes"},
        {"No", "No"},
        {"Maybe", "Maybe"}
    }, "key", "value"))
    @Html.ValidationMessageFor(x => x.ConfirmCeremony)
    <br />
    Attending Reception?<br />
    @Html.DropDownListFor(x => x.ConfirmReception, new SelectList(new Dictionary<string, string>
    {
        {"No Answer", "No Answer"},
        {"Yes", "Yes"},
        {"No", "No"},
        {"Maybe", "Maybe"}
    }, "key", "value"))
    @Html.ValidationMessageFor(x => x.ConfirmReception)
}

这个视图的模型是自定义类型 Guest,当它渲染“出席仪式”和“出席酒会”下拉列表时,它会正确选择存储在模型中的选项。因此,如果我回来编辑这个客人,我可以看到上次编辑时选择的内容,正如预期的那样。这个视图是仅供管理员使用的视图,并且比下一个视图拥有更多的选项。

这是一个精简版的视图,无法工作:

@model AppName.Models.Invite @*Invite has navigation property .Guests which is a collection of custom type Guest*@

@using (Html.BeginForm("SaveRSVP", "Wedding"))
{
    @Html.HiddenFor(x => x.Id)
    <table cellpadding="15px" cellspacing="0">
        <tr>
            <th>
                Name
            </th>
            @if (Model.Guests.Any(x => x.InvitedToCeremony))
            {
                <th>
                    Attending Ceremony?
                </th>
            }
            <th>
                Attending Reception?
            </th>
        </tr>
        @Html.EditorFor(x => x.Guests)
    </table>
    <br />
    <input type="submit" value="Save Responses" />
}

Html.EditorFor(x => x.Guests) 加载包含出席仪式和出席招待会下拉列表的编辑模板。以下是编辑模板:

@model AlexAndNikki.Models.Guest

<tr>
    <td>
        @Html.HiddenFor(x => x.GuestID)
        @Model.FullName()
    </td>
    @if (Model.Invite.Guests.Any(x => x.InvitedToCeremony))
    {
        <td>
            @if (Model.InvitedToCeremony)
            {
                <text>
                @Html.DropDownListFor(x => x.ConfirmCeremony, new SelectList(new Dictionary<string, string>
                        {
                            {"No Answer", "No Answer"},
                            {"Yes", "Yes"},
                            {"No", "No"},
                            {"Maybe", "Maybe"}
                        }, "key", "value"))
                @Html.ValidationMessageFor(x => x.ConfirmCeremony)
                </text>
            }
            else
            {
                <text>
                    No more invites.
                </text>
            }
        </td>
    }
    <td>
        @Html.DropDownListFor(x => x.ConfirmReception, new SelectList(new Dictionary<string, string>
            {
                {"No Answer", "No Answer"},
                {"Yes", "Yes"},
                {"No", "No"},
                {"Maybe", "Maybe"}
            }, "key", "value"))
        @Html.ValidationMessageFor(x => x.ConfirmReception)
    </td>
</tr>

这确实呈现了下拉列表,如果我选择项目并进行POST,则值将正确发送到服务器。但是,下拉列表没有选择正确的值。我甚至在下拉列表上方以纯文本形式显示了编辑器模型中存储的值,以验证正确的值是否传递给了编辑器,并且确实如此。这是下拉列表的问题。我怀疑这与每个附加到邀请的Guest重复使用此编辑器有关,但我无法确定问题所在。
注意:如果我不使用编辑器,而是在视图中执行以下操作,则会发生相同的情况:
@for (int i = 0; i < Model.Guests.Count; i++)
{
                        @Html.DropDownListFor(x => x.Guests.ToList()[i].ConfirmCeremony, new SelectList(new Dictionary<string, string>
                        {
                            {"No Answer", "No Answer"},
                            {"Yes", "Yes"},
                            {"No", "No"},
                            {"Maybe", "Maybe"}
                        }, "key", "value"))
}

同样的问题也存在于这里,下拉列表不会选择框中的值。它只停留在索引0。

模型名称和选择列表名称不能相同,否则浏览器将默认选择第一项。请参阅我的DDL教程http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc和http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx。 - RickAndMSFT
1个回答

4
构造函数有另一个重载,第四个参数是选中的值。

尝试:

    @Html.DropDownListFor(x => x.ConfirmReception, new SelectList(new Dictionary<string, string>
        {
            {"No Answer", "No Answer"},
            {"Yes", "Yes"},
            {"No", "No"},
            {"Maybe", "Maybe"}
        }, "key", "value", Model.ConfirmReception))

编辑:糟糕,尝试一下,假设ConfirmReception是Guest对象的属性。

编辑:我曾经遇到过类似的问题,它没有将值绑定到ddl上。我将发布一些类似于我所做的内容。

编辑页面:

@model Models.OptionViewModel

@using (Html.BeginForm())
{ 
    @Html.EditorFor(r => r.OptionID, new { SelectList = Model.Options })

    @Html.SubmitButton()
}

ViewModel

public class OptionViewModel
{
    [DisplayName("Option")]
    [Required]
    [DataType("DropDownList")]
    public int OptionID { get; set; }

    public SelectList Options { get; private set; }

    public OptionViewModel()
    {
        Options = new SelectList(new OptionRepository().GetAll(), "ID", "Name");
    }

    public OptionViewModel(IOption option)
    {
        this.OptionID = option.OptionID;
        Options = new SelectList(new OptionRepository().GetAll(), "ID", "Name", OptionID);
    }
}

编辑模板:DropDownList.cshtml

@model int

<div class="editor-label">
    @Html.LabelFor(m => m)
</div>
<div class="editor-field">
    @Html.DropDownListFor(m => m, ViewBag.SelectList as SelectList, "Select an item...")
    @Html.ValidationMessageFor(m => m)
</div>

x在当前上下文中不存在。 - Chev
是的,抱歉。请尝试从Model对象获取属性,而不是lambda参数。 - Iain
Model.ConfirmReception 不会改变任何东西。这确实是一个非常奇怪的问题。 - Chev
我之前遇到过类似的问题,所以我只是把我解决问题的方法添加了进去。 - Iain
谢谢。我重构了代码以遵循您的模式,现在它可以正常工作了。 - Chev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接