Assign Attribute to @Html.DropdownList

怎甘沉沦 提交于 2019-12-03 20:15:45

问题


I want to assign some ID attribute name to my Dropdown list which is using data from the ViewBag as,

ViewBag.Group = new SelectList(group, "GroupId", "Name");

and in the View side I have used Razor syntax for showing the Dropdown as,

@Html.DropDownList("Group", "New", new { id="testID" } )

But I am getting the error on this line. Can I just assign ID of This Razor syntax? I know that the ID is generated with the Name "Playlist" But I am having 2 different dropdowns using same ViewBag. Because of that I have to assign different "ID" attribute value for them. How can it be done?


回答1:


You should be using something like this

@Html.DropDownList("Group",null,new{@id="testID"});

because the data for the DropDownList comes from the ViewBag to name Group




回答2:


Try it:

In controller

  ViewBag.EmployeeId = new SelectList(db.tb_employees, "id", "last_name");

In View

@Html.DropDownList("EmployeeId", (IEnumerable<SelectListItem>)ViewBag.EmployeeId,"Show All", new { @class = "form-control" })


来源:https://stackoverflow.com/questions/16828666/assign-attribute-to-html-dropdownlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!