好的,所以我在获取下拉列表时遇到问题,以便在呈现时选择正确的值。它始终默认为索引零。但是,我确实在另一个地方有一个视图,它渲染得很好,并选择正确的值。我不明白其中的区别。
以下是视图的精简版本,可以正常工作:
@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
附加的每个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"))
}
此处存在同样的问题,下拉列表不会选中框中的值。它只停留在索引0。
感谢任何帮助。
答案 0 :(得分:4)
selectlist构造函数还有另一个重载,它将所选值作为第四个参数。
尝试:
@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。我会发布类似于我所做的事情。
<强> 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>