DropDownList没有在ASP.NET MVC中填充正确的值

Che*_*hev 5 c# asp.net asp.net-mvc razor asp.net-mvc-3

好的,所以我在获取下拉列表时遇到问题,以便在呈现时选择正确的值.它始终默认为索引零.但是,我确实在另一个地方有一个视图,它渲染得很好,并选择正确的值.我不明白其中的区别.

这是一个正常工作的视图的修剪版本:

@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)
}
Run Code Online (Sandbox Code Playgroud)

此视图的模型是自定义类型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" />
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

这会渲染下拉列表,如果我选择项目并执行POST,则会将值正确地发送到服务器.但是,下拉列表不会选择正确的值.我甚至在下拉列表上方的纯文本中显示了编辑器模型中存储的值,以验证正确的值是否正在向编辑器发送它们.这是下拉列表的问题.我怀疑这与每个Guest附加的编辑器重复的事实有关,Invite但我无法确定问题.

注意:如果我不使用编辑器,并且在视图中执行类似的操作,则会发生同样的事情:

@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"))
}
Run Code Online (Sandbox Code Playgroud)

此处存在同样的问题,下拉列表不会选中框中的值.它只停留在索引0.

任何帮助表示赞赏.

Iai*_*ain 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))
Run Code Online (Sandbox Code Playgroud)

编辑:哎呀尝试一下,假设ConfirmReception是Guest对象的一个​​属性。

编辑:我有一个类似的问题,它没有将值绑定到 ddl。我只会发布与我所做的类似的内容。

编辑页面:

@model Models.OptionViewModel

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

    @Html.SubmitButton()
}
Run Code Online (Sandbox Code Playgroud)

视图模型

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑器模板: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>
Run Code Online (Sandbox Code Playgroud)