Html.DropDownListFor set selected value
我创建一个@ Html.DropDownListFor并从数据库中填充它。 如何为下拉菜单设置选定的值?
我的看法:
1 2 | @Html.DropDownListFor(m => m.Forms, new SelectList(Model.Forms,"FormsCreatorID","FormName"), "Select a Form", new { @class ="form-control" }) |
我的控制器:
1 2 3 | var forms = db.formscreators.Where(fp => fp.PropertyID == id || fp.PropertyID == 0) .OrderByDescending(x => x.PropertyID).GroupBy(x => x.FormName).Select(x => x.FirstOrDefault()).ToList(); var viewModel = new ListFormsCreator { Forms = forms }; |
我的ViewModel:
1 2 3 4 | public class ListFormsCreator { public List<formscreator> Forms { get; set; } } |
我的数据库模型:
1 2 3 4 5 6 | public partial class formscreator { public int FormsCreatorID { get; set; } public string FormName { get; set; } public int PropertyID { get; set; } } |
谢谢
您应该在控制器上设置" Selected"值来生成" SelectListItem"列表,并通过ViewBag或ViewModel传递它。在我的示例中,为简单起见,我使用了ViewBag。
这是缩短的控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public ActionResult Edit(int? id) { if (id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); Album album = context.Albums.Find(id); if (album == null) { return HttpNotFound(); } ViewBag.GenreId = context.Genres.Select( g => new SelectListItem() { Value = g.GenreId.ToString(), Text = g.Name, Selected = g.GenreId == album.GenreId ? true : false }).ToList(); return View(album); } |
这是缩短的查看代码
1 2 3 | @using MvcMusicStore2017.Models; @model Album @Html.DropDownList("GenreId", null, new { @class ="form-control" }) |
更新viewModel并为下拉选择的值添加一个Int SelectId。
在您的控制器中:
1 | var viewModel = new ListFormsCreator { SelectId = PropertyId, Forms = FormSelectList(forms, PropertyId.ToString()) }; |
我将创建一个传递列表的函数:
1 2 3 4 5 6 7 8 9 10 | public static SelectList FormSelectList(IEnumerable<formscreators> types, string selected = null) { return new SelectList(from f in forms select new SelectListItem { Text = f.FormName, Value = f.FormsCreatorID.ToString() },"Value","Text", selected); } |
并在您的.cshtml中
1 | @Html.DropDownListFor(m => m.PropertyId, Model.forms,"Select a Form", new { @class ="form-control", required ="required" }) |
您应该为视图模型添加另一个属性以存储/传递所选选项。
1 2 3 4 5 | public class ListFormsCreator { public int SelectedFormId { set;get;} public List<formscreator> Forms { get; set; } } |
现在,在GET操作中,您可以设置该值
1 2 3 | var viewModel = new ListFormsCreator() { Forms = forms }; viewModel.SelectedFormId = 2 ; // This will select the option with 2 as FormsCreatorID return View(viewModel); |
在视图中,将具有该属性的lamda表达式用作
1 2 3 4 5 | @model ListFormsCreator @Html.DropDownListFor(m => m.SelectedFormId , new SelectList(Model.Forms,"FormsCreatorID","FormName"), "Select a Form", new { @class ="form-control" }) |
您还可以通过用
1 2 3 4 5 | public class ListFormsCreator { public int SelectedFormId { set;get;} public List<SelectListItem> Forms { get; set; } } |
现在,在GET操作中,可以使用
1 2 3 4 5 6 7 | var viewModel = new ListFormsCreator(); viewModel.Forms = someCollection.Select(a=>new SelectListItem { Value=a.FormsCreatorId.ToString(), Text=a.FormName}) .ToList(); viewModel.SelectedFormId = 2 ; // This will select the option with 2 as FormsCreatorID return View(viewModel); |
假设
现在在视图中的代码要简单得多
1 | @Html.DropDownListFor(m => m.SelectedFormId, Model.Forms ,"Select a Form") |
修改您的ViewModel以添加属性以存储所选的
1 2 3 4 5 6 7 | public class ListFormsCreatorViewModel { [Required] // add or remove the 'Required' attribute as necessary public int? SelectedFormsCreatorId { get; set; } ... } |
如果知道值应该在控制器动作中设置
在
1 | @Html.DropDownListFor( m => m.SelectedFormsCreatorId, this.Model.Forms ); |