简体   繁体   English

获取 System.Web.Mvc.SelectListItem 作为 DropDownList 的选项

[英]Getting System.Web.Mvc.SelectListItem as options of DropDownList

I have a ViewBag.CategoryList which contains ViewBag.CategoryList = new SelectList(Category.GetCategories());我有一个ViewBag.CategoryList ,其中包含ViewBag.CategoryList = new SelectList(Category.GetCategories());

My View:我的看法:

@Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.CategoryList), "Select Category", htmlAttributes: new {@class = "form-control"})

What I'm getting is <option>System.Web.Mvc.SelectListItem</option> instead of my strings.我得到的是<option>System.Web.Mvc.SelectListItem</option>而不是我的字符串。 Debugger shows that the ViewBag does have a list of strings.调试器显示 ViewBag 确实有一个字符串列表。

Edit: Fixed it by replacing the view with:编辑:通过将视图替换为以下内容来修复它:

@Html.DropDownListFor(model => model.Category, (IEnumerable)(ViewBag.CategoryList), "Select Category", htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })

actually give a select list instead of a string list from the controller.实际上给出一个 select 列表而不是 controller 中的字符串列表。

ViewBag.CategoryList = Category.GetCategories().Select(r => new SelectListItem()
            {
                Value = r,
                Text = r
            })
@Html.DropDownListFor(model => model.Category, new SelectList(ViewBag.CategoryList, "Value", "Text"), "Select Category", htmlAttributes: new {@class = "form-control"}) 

try this尝试这个

var list=Category.GetCategories();
ViewBag.CategoryList = list.Select(i => new SelectListItem
            {
                Value = i.Id.ToString(),
                Text = i.Name
            })

and view并查看

@Html.DropDownListFor(model => model.Category, @ViewBag.CategoryList,"Select Category", htmlAttributes: new { @class = "form-control" })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在DropDownList中获取“System.Web.Mvc.SelectListItem”? - Why I am getting “System.Web.Mvc.SelectListItem” in my DropDownList? 为什么我的Html.DropDownList给我一个用“ System.Web.Mvc.SelectListItem”字符串填充的列表? - Why does my Html.DropDownList give me a list populated with “System.Web.Mvc.SelectListItem” strings? 为什么我的Html.DropDownList会产生一个列表,其中填充了“ System.Web.Mvc.SelectListItem”字符串? - Why would my Html.DropDownList yield a list filled with “System.Web.Mvc.SelectListItem” strings? 从选择列表查询填充下拉列表在下拉列表中返回 System.Web.Mvc.SelectListItem - Populating dropdown from selectlist query returns System.Web.Mvc.SelectListItem in dropdownlist 为什么这会呈现为“System.Web.Mvc.SelectListItem”的列表? - Why does this render as a list of “System.Web.Mvc.SelectListItem”s? &#39;System.Web.Mvc.SelectListItem&#39; 不包含名为 &#39;id&#39; #2 的属性 - 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'id' #2 ''System.Web.Mvc.SelectListItem' 不包含'string'的定义' - ''System.Web.Mvc.SelectListItem' does not contain a definition for 'string'' System.Web.Mvc.SelectListItem不能用作参数值 - System.Web.Mvc.SelectListItem cannot be used as a parameter value 为什么我的 DropDownList 显示数据项类型 (System.Web.Mvc.SelectListItem) 而不是数据的值? - Why does my DropDownList display the data item type (System.Web.Mvc.SelectListItem) instead of the data's value? 从类型“System.String”到类型“System.Web.Mvc.SelectListItem”的参数转换失败 - The parameter conversion from type 'System.String' to type 'System.Web.Mvc.SelectListItem' failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM