本文介绍了asp.net MVC 3 pre-选择Html.DropDownListFor的书呆子晚餐不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 学习一下下拉列表中,我试着给的NerdDinner添加RSVP创建页面中的斯科特谷的一个Html.DropDownListFor博客列出可用晚餐。 Learning about dropdown lists, Im trying to add a RSVP create page for nerddinner as in Scott Gu's blog with a Html.DropDownListFor listing available dinners. 我可以得到填充的下拉列表中,但我不能让下拉至pre选择数值(样本晚餐2)我想要的。 IM使用initilizer种子的分贝数晚宴的对象。该数据库是使用EF'code第一种方法'SQL CE 4。对不起,我知道这是一个V常见的问题和仇恨来问,但说实话已经花了相当长的一段时间这一点,但不能得到它的工作:I can get the dropdown list populated but I cannot get the dropdown to pre select the value ("Sample Dinner 2") I want. Im using an initilizer to seed a few dinner objects in the db. The database is sql ce 4 using a EF 'code first approach'. Sorry I know this is a v common problem and hate to ask, but honestly have spent quite some time on this but cant get it to work: 视图模型 public class RSVPViewModel{ public SelectList DinnersList { get; set; } public RSVP Rsvp { get; set; } public string SelectedItem { get; set; }} 控制器 // //GET: /RSVP/Create public ActionResult Create() { RSVP rsvp = new RSVP(); string selected = "Sample Dinner 2"; var typeList = new SelectList(dbc.Dinners.ToList(), "DinnerID", "Title", selected); var viewModel = new RSVPViewModel { DinnersList = typeList, Rsvp = rsvp, SelectedItem = selected }; return View("Create", viewModel); } 查看 @Html.DropDownListFor(model => model.Rsvp.DinnerID, Model.DinnersList) HTML结果 <select data-val="true" data-val-number="The field DinnerID must be a number." data-val-required="The DinnerID field is required." id="Rsvp_DinnerID" name="Rsvp.DinnerID"> <option value="1">Sample Dinner 1</option> <option value="2">Sample Dinner 2</option></select>那么是不是preselecting与价值样品2晚餐页面加载时的DropDownList。列表显示确定并设置正确的DinnerID当我做出选择并点击提交。 So is not preselecting the dropdownlist with the value "Sample Dinner 2" when the page loads. The list displays ok and sets the correct DinnerID when I make a selection and click Submit. 尝试,这也:@Html.DropDownListFor(x => x.SelectedItem, Model.DinnersList)但犯规设置或绑定到Rsvp.DinnerID。but doesnt set or bind to Rsvp.DinnerID.从列表中,但犯规绑定(或设置Rsvp.DinnerID)这preselects This preselects from the list but doesnt bind (or set Rsvp.DinnerID)@Html.DropDownList("DinnersList")我想保持它MVC3所以想用视图模型方法(无的ViewData)和preferably使用Html.DropDownListFor强类型来实现(不Html.DropDownList).Viewbag似乎没有必要为这种情况。I want to keep it mvc3 so want to implement with strong type using ViewModel approach (no ViewData) and preferably using Html.DropDownListFor (not Html.DropDownList).Viewbag seems unnecessary for this case.谢谢! EDIT1 想我应该使用selectListItems的选择列表我尝试这样冗长的方法:Thinking I should be using a selectList of selectListItems I tried this verbose approach :RSVP rsvp = new RSVP();string selected = "2";List<SelectListItem> dinners = new List<SelectListItem>();foreach (Dinner dinner in dbc.Dinners.ToList()){ SelectListItem slDinner = new SelectListItem(); slDinner.Value = dinner.DinnerID.ToString(); slDinner.Text = dinner.Title; slDinner.Selected = (slDinner.Value == selected); dinners.Add(slDinner); } var dinnersList = new SelectList(dinners, "Value", "Text", selected); var viewModel = new RSVPViewModel { DinnersList = dinnersList, Rsvp = rsvp, SelectedItem = selected };但仍然没有工作。我应该让使用的视图模型SelectedItem属性的:@ Html.DropDownListFor ..不知何故?是这样的:However still no work. Should I be making use of the ViewModel SelectedItem property in: @Html.DropDownListFor.. somehow? Something like :@Html.DropDownListFor(x => x.SelectedItem, Model.DinnersList)但如何我得到一个选择的值来设置Rsvp.DinnerID。我觉得这就是所谓的绑定。but how to I get a selected value to set Rsvp.DinnerID. I think thats called binding.推荐答案这里和阅读后这里,我终于明白如何HtmlDropDownlistFor自动在下拉列表中选择正确的项目基于模型 - 选择在RSVP一个dinnerID(外键dinner.dinnerID)将导致Dinner.DinnerIDs以pre含有下拉列表中选择该值。没有必要的没有的我觉得在了selectedValue选择列表或视图模型。After reading here and here, I finally understand how HtmlDropDownlistFor automatically selects the correct item in the dropdown based on your model - selecting a dinnerID in RSVP (foreign key to dinner.dinnerID) will cause the dropdown containing list of Dinner.DinnerIDs to pre select that value. No need yet I think for selectedValue in the SelectList or ViewModel. 解决方案: // //GET: /RSVP/Create public ActionResult Create() { //automatically preselects matching DinnerID in the Dinner dropdownlist var rsvp = new RSVP {DinnerID = 2 }; var typeList = new SelectList(dbc.Dinners.ToList(), "DinnerID", "Title"); var viewModel = new RSVPViewModel { DinnersList = typeList, Rsvp = rsvp}; return View("Create", viewModel); } 这篇关于asp.net MVC 3 pre-选择Html.DropDownListFor的书呆子晚餐不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-25 16:55