Asp.net mvc 4 @Html.dropdownlist的选定值表示不工作

Asp.net mvc 4 @Html.dropdownlist的选定值表示不工作,asp.net-mvc-4,Asp.net Mvc 4,未显示选定的drowdown值 @Html.DropDownListFor(x => x.AgeGroup, ViewBag.AgeGroup as SelectList, "Please select", new {@class = "form-control", id = "ddl_InvAgeGroup", name = "AgeGroup"} ) 要实现所需效果,视图模型上可以有两个属性:一个包含选定值,另一个包含可能值列表: public class MyViewModel {

未显示选定的drowdown值

@Html.DropDownListFor(x => x.AgeGroup, ViewBag.AgeGroup as SelectList, "Please select", new {@class = "form-control", id = "ddl_InvAgeGroup", name = "AgeGroup"} )

要实现所需效果,视图模型上可以有两个属性:一个包含选定值,另一个包含可能值列表:

public class MyViewModel
{
    public string AgeGroup { get; set; }
    public IEnumerable<SelectListItem> AgeGroups { get; set; }
}
最后,在你看来:

@Html.DropDownListFor(
    x => x.AgeGroup, 
    Model.AgeGroups, 
    "Please select", 
    new { @class = "form-control", id = "ddl_InvAgeGroup", name = "AgeGroup" }
)

格式为@Html.DropDownListm=>m.yourProperty,Model.YourSelectList,请选择,新建{…}-您需要绑定到的属性和SelectList的属性,如果该属性的值与其中一个选项的值匹配,则会选择该属性修改我的代码,但它不会显示所选的值,而是显示两倍的下拉值您似乎在使用相同的属性进行集合和所选值:AgeGroup。这不是这个助手应该被使用的方式。视图模型上需要两个属性:一个标量类型将保存选定的值,另一个IEnumerable属性将保存下拉列表中可能显示的值列表。但是我需要动态数据存储,只需将动态数据投影到视图模型的AgeGroups集合属性中,与填充ViewBag.age组的方式相同。
@Html.DropDownListFor(
    x => x.AgeGroup, 
    Model.AgeGroups, 
    "Please select", 
    new { @class = "form-control", id = "ddl_InvAgeGroup", name = "AgeGroup" }
)