Axel Ki
Axel Ki

Reputation: 13

Dropdown for filter, add new item in list

Have dropdown list, which elements are defined in controller

ViewBag.AudienceFilter= new SelectList(db.Requests, "Id", "Audience");

and this dropdown on view:

@Html.DropDownList("AudienceFilter", null, htmlAttributes: new { @class = "form-control" })

this how the result looks like http://snag.gy/dkuGH.jpg

need this to make a filter, and i need one more item in dropdown: "All", what and where do i need to edit, to add this item?

Upvotes: 0

Views: 97

Answers (1)

Inspector Squirrel
Inspector Squirrel

Reputation: 2548

To add the item to your SelectList just do:

List<SelectListItem> list = new SelectList(db.Requests, "Id", "Audience").ToList();

var listItem = new SelectListItem();
listItem.Text = "All";
listItem.Value = "All";

list.Add(listItem);

ViewBag.AudienceFilter = new SelectList(list);

I'll add the other half of the answer when you clarify what you want.

Upvotes: 2

Related Questions