在asp.net mvc中使用selectlist的dropdownbox的选定值
问题描述:
我有下面的aspnet mvc中的dropdownbox选择列表。因为“SelectList”需要3个参数一个用于值,一个用于文本,另一个用于选定的值,这里我是一个EditEmployee Action控制器,所以当显示编辑页面时,我想在下拉框中显示一个selectvalue,米没有得到什么,我应该通过第三个参数,因为它要求一个对象的选定值。在asp.net mvc中使用selectlist的dropdownbox的选定值
ViewData["DepartmentList"] = new SelectList(DepartmentRepository.GetDepartmentsBySchoolIdInList(ViewData["schoolId"].ToString()),"DepartmentId","DepartmentTitle");
这里是
=Html.DropDownList("DepartmentList")
答
var deptList = DepartmentRepository.GetDepartmentsBySchoolIdInList(ViewData["schoolId"].ToString());
ViewData["DepartmentList"] = new SelectList(DepartmentRepository.GetDepartmentsBySchoolIdInList(deptList,"DepartmentId",deptList.First());
见我DDL的教程认为http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-和-jquery/using-the-dropdownlist-helper-with-aspnet-mvc和http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net- mvc.aspx – RickAndMSFT 2012-02-14 19:25:07