Manish Pawar Manish Pawar - 2 months ago 6
ASP.NET (C#) Question

How to bind drowpdown list with one static value and rest from database

I am working with mvc I want to add a

dropdown list
that holds one static value and other values coming from database

@Html.DropDownListFor(Function(model) model.nDepartmentID,
New SelectList(ViewBag.txt, "Value", "Text"), New With
{Key .[class] = "select1",Key .style = "width: 150px;"})


Use below code

@Html.DropDownListFor(m => m.nDepartmentID, (SelectList)ViewBag.DepartmentList, "Select Any Department", new {@class="select1",@style="width: 150px;" })

Your controller Action will be

public ActionResult ShowPage()
      var deptmnts=db.Departments.ToList();
      ViewBag.DepartmentList=new SelectList(deptmnts,"ID","DepartmentName");
      retun View();