-1

Can anyone help me with this. The first item on list is always empty , so it shouldn't be like that :

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

Code in controller :

ViewBag.EMRecordType_ = new SelectList(db.EMRecords, "EMRecordType_", "EMRecordType_");
ramzan ali
  • 471
  • 4
  • 14
ddgg ddgg
  • 13
  • 7
  • Because you have `""` as the 3rd parameter (which generates an option with that text, and a value of `null`). Just remove it - `@Html.DropDownList("EMRecordType_", null, new { @class = "form-control" })` –  Nov 23 '17 at 10:32
  • Now the first element on the list goes up and stays as default value , i want the dropdownlist default value to be empty . – ddgg ddgg Nov 23 '17 at 10:37
  • Yes of course - your not binding to anything by using that overload, and if you do not bind to anything, the first option will always be selected. Its not clear now what you want to do, but that is the worst possible way to generate a dropdownlist. To do it correctly, refer to the code in [this Q/A](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) –  Nov 23 '17 at 10:41

2 Answers2

0

I have replicated your Issue it is working fine

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


 public ActionResult Demo()
    {
        try
        {
            List<DemoModel> li = new List<Models.DemoModel>()
        {
            new DemoModel { ID = "1" , Value = "DemoModel1"},
            new DemoModel { ID = "2" , Value = "DemoModel2"},
            new DemoModel { ID = "3" , Value = "DemoModel3"},
            new DemoModel { ID = "4" , Value = "DemoModel4"},
            new DemoModel { ID = "5" , Value = "DemoModel5"},
            new DemoModel { ID = "6" , Value = "DemoModel6"},
            new DemoModel { ID = "7" , Value = "DemoModel7"},
        };

            ViewBag.EMRecordType_ = new SelectList(li, "ID", "Value");

            return View();
        }
        catch (Exception)
        {
            throw;
        }
    }

Output

enter image description here

Saineshwar Bageri - MVP
  • 3,851
  • 7
  • 41
  • 47
  • I have edited the question , i added a screenshot , with this code , the first item list now stands as default value .I want the default value to be SELECT , and on the list to have the items from database . – ddgg ddgg Nov 23 '17 at 13:07
  • @Html.DropDownList("empRecordTypeDropDown", (IEnumerable)ViewBag.EMRecordType_, "Please Select", new {@class="form-control"}) this works fine , but the problem is that when i press arrow , the first element on list is Please Select , then the others , i want to remove this first time , nor please select , nor empty first element , ..only the list elements from database – ddgg ddgg Nov 24 '17 at 10:20
0

As far I understand you wanted to add an empty default value,like "Please Select..." to the drop-down list.

Try this to get the following output..

@Html.DropDownList("empRecordTypeDropDown", (IEnumerable<SelectListItem>)ViewBag.EMRecordType_, "Please Select", new {@class="form-control"})

Output

If you want to add totally empty option then use

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

and the output

  • The first parameter takes a not null string value which is used as an id and namefor the drop-down.
  • As the @Html.Dropdown requires a collection of System.Web.Mvc.SelectListItem so you have to cast the dynamic ViewBag.
ramzan ali
  • 471
  • 4
  • 14
  • @Html.DropDownList("empRecordTypeDropDown", (IEnumerable)ViewBag.EMRecordType_, "Please Select", new {@class="form-control"}) this works fine , but the problem is that when i press arrow , the first element on list is Please Select , then the others , i want to remove this first time , nor please select , nor empty first element , ..only the list elements from database – ddgg ddgg Nov 24 '17 at 10:20