在asp.net mvc3中如何在回发后保持下拉列表选中项目.
解决方法
做这样的事情:
[HttpPost] public ActionResult Create(FormCollection collection) { if (TryUpdateModel(yourmodel)) { //your logic return RedirectToAction("Index"); } int selectedvalue = Convert.ToInt32(collection["selectedValue"]); ViewData["dropdownlist"] = new SelectList(getAllEvents.ToList(),"EventID","Name",selectedvalue);// your dropdownlist return View(); }
并在视图中:
<%: Html.DropDownListFor(model => model.ProductID,(SelectList)ViewData["dropdownlist"])%>