繁体   English   中英

不选择预设值的ASP.NET MVC DropDownList

[英]ASP.NET MVC DropDownListFor Not Selecting Prepoulated Values

首先,我检查了所有相关问题,但是没有一个解决我的问题。

我正在使用具有多个属性的ASP.NET MVC Html帮助程序Dropdownlistfor,如下所示。

@Html.DropDownListFor(m => m.SelectedPeople, 
                       Model.People, 
                       new { multiple = "multiple" })

/* Viewmodel for my view */
public class MyModel
{
    public IEnumerable<SelectListItem> People       { get; set; }
    public IEnumerable<string> SelectedPeople       { get; set; }
} 

如果我不使用某些选定值预填充下拉列表。 工作正常。 所选值将毫无问题地发布到操作方法。

但是,如果我要预填充下拉列表,则它不起作用。 我按如下方式填充下拉列表,

model.People = new[] 
{
    new SelectListItem { Value = "1", Text = "group 1" },
    new SelectListItem { Value = "2", Text = "group 2" },
    new SelectListItem { Value = "3", Text = "group 3" },
 };

model.SelectedPeople = new[] { "2", "3" };

通过不起作用,我的意思是在DropDownList中未选择SelectListItems 2和3。

你有没有尝试过:

model.People = new[] 
{
    new SelectListItem { Value = "1", Text = "group 1" },
    new SelectListItem { Value = "2", Text = "group 2", Selected = true },
    new SelectListItem { Value = "3", Text = "group 3", Selected = true },
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM