简体   繁体   English

在ASP.Net MVC5中为DropdownList附加数据绑定项的替代方法是什么

[英]What is the alternative of append data bound items for DropdownList in ASP.Net MVC5

      @Html.DropDownListFor(m => m.Name,(IEnumerable<SelectListItem>)ViewBag.list,
      new { @style = "width:100%", @class = "form-control" }) 

This can help to set the selected item only at run time. 这有助于仅在运行时设置所选项目。 How to add few more items at run time (inside controller ) 如何在运行时添加几个项目(在控制器内部)

Simple change the source before assigning it to your ViewBag. 在将源分配给您的ViewBag之前,只需对其进行更改。

eg: 例如:

IEnumerable<SelectListItem> yourSource = //...;
var list = yourSource.ToList();
list.Add(...);
ViewBag.list = list;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM