0

我有一个带有下拉菜单的编辑器模板:

 @Html.DropDownListFor(m => m.AdmissionState, new List<SelectListItem>()
                                                   {
                                                       new SelectListItem()
                                                           {
                                                               Text = "1",
                                                               Value = "1"
                                                           },
                                                       new SelectListItem()
                                                           {
                                                               Text = "2",
                                                               Value = "2"
                                                           },
                                                       new SelectListItem()
                                                           {
                                                               Text = "3",
                                                               Value = "3"
                                                           },                   
                                                   }, "- Select -")

我从视图中调用它:

@Html.EditorFor(m => m.Licenses)

在控制器中,我有以下代码:

profile.Licenses.Add(new AttorneyLicenseModel()
        {
            AdmissionDate = "06/14/2012",
            AdmissionState = "2",
            AdmissionNumber = "asd",
        });
        profile.Licenses.Add(new AttorneyLicenseModel()
        {
            AdmissionDate = "07/16/2012",
            AdmissionState = "5",
            AdmissionNumber = "qwe",
        });

对于每个许可证,我都看到了从模型中填充数据的字段,但下拉列表中没有 - 选中了默认值。我究竟做错了什么?当然,带有值的代码非常简化

4

1 回答 1

1

试试这样:

@Html.DropDownListFor(
    m => m.AdmissionState, 
    new SelectList(
        new[] 
        {
            new { Value = "1", Text = "1" },
            new { Value = "2", Text = "2" },
            new { Value = "3", Text = "3" },
        }, 
        "Value", 
        "Text", 
        Model.AdmissionState
    ),
    "- Select -"
)

如果您有兴趣了解为什么我的代码有效而您的代码无效,您可以阅读我昨天为类似问题提供的以下答案:ASP.NET MVC DropDownListFor not selection value from model

于 2012-06-19T16:53:21.717 回答