DROPDOWNLIST不与ASP.NET MVC正确的值填充 [英] DropDownList not populating with correct value in ASP.NET MVC

查看:199
本文介绍了DROPDOWNLIST不与ASP.NET MVC正确的值填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我有得到一个下拉列表中选择合适的值时,它呈现的问题。它总是默认为指数为零。不过,我有在它呈现就好了,并选择正确的价值另一个地方的景色。我不明白的区别。

Okay, so I am having an issue getting a drop down list to choose the right value when it renders. It always defaults to index zero. However, I do have a view in another place where it renders just fine and selects the right value. I don't understand the difference.

下面是视图的下调版本,正常工作

Here is a trimmed down version of the view that works correctly:

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

该视图的模式是自定义类型访客的,当它呈现出席仪式并出席酒会下拉列表它正确选择存储在模型中的选项。所以,如果我回来编辑这个客人,我可以看到无论我选择了我最后一次编辑它,符合市场预期。这种观点是只有管理员视图,并具有比下查看更多选项。

This view's model is of custom type Guest and when it renders the Attending Ceremony and Attending Reception drop down lists it correctly selects the option that is stored in the model. So if I come back to edit this guest I can see whatever I selected the last time I edited it, as expected. This view is an admin-only view and has more options than the next view.

下面是不起作用的视图的下调版本:

Here is a trimmed down version of the view that doesn't work:

@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 =&GT; x.Guests)加载包含下拉列表出席仪式并出席酒会的编辑模板。下面是编辑模板:

Html.EditorFor(x => x.Guests) loads an editor template that contains the drop down lists for Attending Ceremony and Attending Reception. Here is the editor template:

@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的值正确发送到服务器。然而,在下拉列表中没有选择正确的值。我甚至显示存储在模型在下拉列表上方的纯文本编辑器来验证正确的值做它的编辑和他们的价值。它是用下拉列表中的问题。我怀疑它是值得做的事实,这个编辑器重复每个访客连接到邀请,但我不能确定问题。

This does render the drop down lists and if I select items and do a POST the values are sent correctly to the server. However, the drop down lists do not select the correct value. I have even displayed the value stored in the model for the editor in plain text above the drop down list to verify that the correct values are making it to the editor and they are. It is a problem with the drop down lists. I suspect it is something to do with the fact that this editor is repeated for every Guest attached to the Invite but I cannot pinpoint the issue.

注意:这同样的事情发生,如果我不使用编辑器,而是做这样的事情的看法:

NOTE: This same thing happens if I don't use an editor, and instead do something like this in the 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。

The same problem exists here, the drop down list will not select the value in the box. It just stays at index 0.

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

有另一种重载选择列表的构造函数选定值作为第四个参数。

There is another overload to the selectlist constructor that takes the selected value as the fourth parameter.

尝试:

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

Oops try that, assuming ConfirmReception is a property of the Guest object.

编辑:我也有类似的问题,它不具约束力的价值的DDL。我只是张贴类似我做了什么事。

I had a similar problem with it not binding the value to the ddl. I'll just post something similar to what i had done.

Editorpage:

@model Models.OptionViewModel

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

    @Html.SubmitButton()
}

视图模型

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

EditorTemplate: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>

这篇关于DROPDOWNLIST不与ASP.NET MVC正确的值填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆