模型 公共类ViewModel{public int Job {获取;放;}公共列表< SelectListItem>乔布斯{get;放;}} 控制器 [HttpGet]公共ActionResult ControllerName(){var model = new ViewModel();model.Jobs = new List< SelectListItem>();model.Jobs.Add(new SelectListItem(){Text ="Email",Value ="1",Selected = false});//在此处分配其他SelectListItem列表值返回View(model);} 查看 @model ViewModel@using(Html.BeginForm("ActionName","ControllerName",FormMethod.Post)){< div style ="text-align:center">< div class ="form-group">@ Html.DropDownListFor(m => m.Job,Model.Jobs)</div></div>} I've got a view with a few properties and want to create a drop down box for it. So far, I've created a list of selectListItems on my controller and added values to it and at the moment, it works and displays the items in the drop down list. However, my problem is that it's not linked at all to the property I have in my view model. How can I amend this?controller: List<SelectListItem> jobs = new List<SelectListItem>(); SelectListItem job1 = new SelectListItem() { Text = "Email", Value = "1", Selected = false }; SelectListItem job2 = new SelectListItem() { Text = "OtherJob", Value = "2", Selected = false }; SelectListItem job3 = new SelectListItem() { Text = "OtherJob2", Value = "3", Selected = false }; SelectListItem job4 = new SelectListItem() { Text = "OtherJob3", Value = "4", Selected = false }; SelectListItem job5 = new SelectListItem() { Text = "OtherJob4", Value = "5", Selected = false }; SelectListItem job6 = new SelectListItem() { Text = "OtherJob5", Value = "6", Selected = false }; SelectListItem job7 = new SelectListItem() { Text = "OtherJob6", Value = "7", Selected = false }; SelectListItem job8 = new SelectListItem() { Text = "OtherJob7", Value = "8", Selected = false }; jobs.Add(job1); jobs.Add(job2); jobs.Add(job3); jobs.Add(job4); jobs.Add(job5); jobs.Add(job6); jobs.Add(job7); jobs.Add(job8);ViewBag.jobs = jobs; return View();ViewModel:public object Job { get; set; }View: @using (Html.BeginForm(FormMethod.Post)){ <center> <div class="form-group"> @Html.DropDownList("jobs", (IEnumerable <SelectListItem>)ViewBag.jobs) </div> </center> 解决方案 Your name argument passed to DropDownList is wrong. It should be same as viewmodel property name to bind properly:@Html.DropDownList("Job", (IEnumerable<SelectListItem>)ViewBag.jobs)Since you have strongly-typed viewmodel, better to use DropDownListFor helper and change corresponding viewmodel property type to be same as SelectListItem.Value property type. Also you can eliminate cast from ViewBag usage by using a property which has type of IEnumerable<SelectListItem> and assign it directly to DropDownListFor:Modelpublic class ViewModel{ public int Job { get; set; } public List<SelectListItem> Jobs { get; set; }}Controller[HttpGet]public ActionResult ControllerName(){ var model = new ViewModel(); model.Jobs = new List<SelectListItem>(); model.Jobs.Add(new SelectListItem() { Text = "Email", Value = "1", Selected = false }); // assign other SelectListItem list values here return View(model);}View@model ViewModel@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post)) { <div style="text-align:center"> <div class="form-group"> @Html.DropDownListFor(m => m.Job, Model.Jobs) </div> </div>} 这篇关于使用视图模型属性填充一个下拉列表:MVC 5,Razor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-13 17:04