Tag: selectlist

通用枚举到SelectList扩展方法

我需要在项目中的任何Enum中创建一个SelectList 。 我有下面的代码,我从特定的枚举创建一个选择列表,但我想为任何枚举创建一个扩展方法。 此示例检索每个Enum值上的DescriptionAttribute值 var list = new SelectList( Enum.GetValues(typeof(eChargeType)) .Cast() .Select(n => new { id = (int)n, label = n.ToString() }), “id”, “label”, charge.type_id); 参考这篇文章 ,我该如何处理? public static void ToSelectList(this Enum e) { // code here }

MVC DropDownList选中的值不起作用

我正在使用MVC 5。 我有我的ViewBag列表 ViewBag.TitleList = new SelectList((new string[] { “Mr”, “Miss”, “Ms”, “Mrs” }), contact.Title); //contact.Title has selected value. 然后我尝试将数组转换为SelectListItem ( to no avail) 在视图上它看起来像 @Html.DropDownListFor(model => model.Title, ViewBag.TitleList as SelectList, “Select”) 我也试过了 @Html.DropDownList(“Title”, ViewBag.TitleList as SelectList, “Select”) 列表已成功加载,但未Selected Value is Not selected 。 如何解决这个问题? 更新罪魁祸首是ViewBag.Title匹配我的模型。标题。 将我的模型属性重命名为其他内容并且有效。 Arrgh!

我无法从表中填充DropDownList。 EF和MVC4

我相信这会在我的HomeController中创建一个列表。 但不确定是什么调用它或它在Controller旁边的位置可能是第一个Add ActionResult(GET方法)。 public static IEnumerable items() { using (oesacEntities_compact db = new oesacEntities_compact()) { var query = from s in db.tblSponsors select new { s.SponsorID, s.BizName }; return query.AsEnumerable() .Select(x => new SelectListItem { Value=x.SponsorID.ToString(), Text = x.BizName }).ToList(); } } 我似乎无法将其发送到添加视图或从添加视图引用它: @Html.DropDownListFor(model => model.SponsorID,IEnumerable SelectList); 在其他编码语言中似乎很简单。 我想填充一个下载大约有200个赞助商ID的下拉值,BizNames用于文本。 至少现在。 当我想要显示具有所选值的编辑视图时,上帝帮助我。 谢谢你的stackoverflow