c# - DropDownList 未在 ASP.NET MVC 中填充正确的值

标签 c# asp.net asp.net-mvc asp.net-mvc-3 razor

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

这是一个可以正常工作的精简版 View :

@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)
}

此 View 的模型是自定义类型 Guest,当它呈现 Attenting Ceremony 和 Attenting Reception 下拉列表时,它会正确选择存储在模型中的选项。因此,如果我回来编辑此 guest ,我可以看到我上次编辑时选择的任何内容,正如预期的那样。此 View 是仅限管理员的 View ,并且比下一个 View 具有更多选项。

这是一个无效的 View 的精简版本:

@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,则值会正确发送到服务器。但是,下拉列表没有选择正确的值。我什至在下拉列表上方以纯文本形式为编辑器显示了存储在模型中的值,以验证正确的值是否正在传递给编辑器,它们确实如此。这是下拉列表的问题。我怀疑这与以下事实有关:对于附加到 Invite 的每个 Guest 都会重复此编辑器,但我无法查明问题所在。

注意:如果我不使用编辑器,而是在 View 中做类似的事情,也会发生同样的事情:

@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。

最佳答案

选择列表构造函数还有另一个重载,它将所选值作为第四个参数。

尝试:

    @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 对象的属性。

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

编辑页面:

@model Models.OptionViewModel

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

    @Html.SubmitButton()
}

View 模型

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>

关于c# - DropDownList 未在 ASP.NET MVC 中填充正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5072905/

相关文章:

c# - ScrollView 错误

c# - 异步方法没有可用的扩展方法,但结果变量有

c# - Skype 邀请信息

c# - 将 LastModified 日期与用户详细信息一起保存

c# - Microsoft.Net.Compilers 的目的是什么?

c# - 防止 TestInitialize 为一个 TestMethod 方法运行

asp.net Web 表单国际化

asp.net - 注册自定义控件失败

asp.net-mvc - 如何在 MVC 中更改由 @Html 帮助程序生成的 'data-val-number' 消息验证

c# - AngularJS Web Api AntiForgeryToken CSRF