Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign Attribute to @Html.DropdownList

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?

like image 969
Rahul RJ Avatar asked May 30 '13 05:05

Rahul RJ


People also ask

What is Html DropDownListFor?

The Html. DropDownListFor<TModel,TProperty> extension method is a strongly typed extension method generates <select> element for the property specified using a lambda expression.


2 Answers

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

like image 75
Saravanan Avatar answered Oct 14 '22 10:10

Saravanan


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" })
like image 32
Aleksey Radzhabov Avatar answered Oct 14 '22 10:10

Aleksey Radzhabov