locked
DropDownList not working RRS feed

  • Question

  • User-1330485181 posted

    Hi,

    I have a table named ms_sector with fields sectorid & sectorname. I want to display data from that table into DropDownList.

    I created a controller named DropDownListController.cs the code likes this below

    public class DropDownListController : Controller
        {
            // GET: DropDownList
            public ActionResult Index()
            {
                
                #region Database with EF
    
                using (IDXDataEntities1 db = new IDXDataEntities1())
                {
                    var fromDatabaseEF = new SelectList(db.ms_sector.ToList(), "sectorid", "sectorname");
                    ViewData["DBSector"] = fromDatabaseEF;
                }
    
                #endregion
                return View();
            }
    
        }

    On the Views code, likes this below:

    <div class="form-group">
                @Html.LabelFor(model => model.sectorid, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownList("sectorid", (IEnumerable<SelectListItem>)ViewData["DBSector"])
                    @Html.ValidationMessageFor(model => model.sectorid, "", new { @class = "text-danger" })
                </div>
            </div>

    When I run the program, i shows an error like this below

    Server Error in '/' Application.


    There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'sectorid'.

       Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.InvalidOperationException: There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'sectorid'.

    Source Error:

    Line 34:             @Html.LabelFor(model => model.sectorid, htmlAttributes: new { @class = "control-label col-md-2" })
    Line 35:             <div class="col-md-10">
    Line 36:                 @Html.DropDownList("sectorid", (IEnumerable<SelectListItem>)ViewData["DBSector"])
    Line 37:                 @Html.ValidationMessageFor(model => model.sectorid, "", new { @class = "text-danger" })
    Line 38:             </div>

    Does anyone know what is missing ?

    Thank you.

    Regards,

    Sentoso

    Friday, February 22, 2019 10:16 AM

Answers

  • User632428103 posted

    Hello sentoso,

    a dropdown contains everytime a text and an id you need to specify it ..

    something like this :

    Html.DropDownList("sectorid", 
        new SelectList((IEnumerable) ViewData[" DBSelector "]), " sectorid ", " sectorname")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 22, 2019 10:24 AM
  • User1520731567 posted

    Hello sentoso,

    I wrote the code like the code below and it works, but when I clicked the dropdownlist it only show for Id (sectorid) not the Text (sectorname)

    @Html.DropDownList("sectorid", new SelectList((System.Collections.IEnumerable)ViewData["DBSector"]), "sectorid", "sectorname")

    How to display Text (sectorname) ?

    You only need to exchange the positions of sectorid and sectorname.

    like:

    @Html.DropDownList("sectorid", new SelectList((System.Collections.IEnumerable)ViewData["DBSector"]),"sectorname","sectorid")

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 25, 2019 8:47 AM

All replies

  • User632428103 posted

    Hello sentoso,

    a dropdown contains everytime a text and an id you need to specify it ..

    something like this :

    Html.DropDownList("sectorid", 
        new SelectList((IEnumerable) ViewData[" DBSelector "]), " sectorid ", " sectorname")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 22, 2019 10:24 AM
  • User-2054057000 posted

    Change your View code from:

    @Html.DropDownList("sectorid", (IEnumerable<SelectListItem>)ViewData["DBSector"])

    To

    @Html.DropDownList("ddlname", ViewBag.DBSector as IEnumerable)

    Reference tutorial - Binding Dropdownlist in asp.net mvc

    Thanks

    Friday, February 22, 2019 10:34 AM
  • User-1330485181 posted

    Thanks Jimmy, for the idea,

    I wrote the code like the code below and it works, but when I clicked the dropdownlist it only show for Id (sectorid) not the Text (sectorname)

    @Html.DropDownList("sectorid", new SelectList((System.Collections.IEnumerable)ViewData["DBSector"]), "sectorid", "sectorname")

    How to display Text (sectorname) ?

    Regards,

    Sentoso

    Sunday, February 24, 2019 2:23 AM
  • User1520731567 posted

    Hello sentoso,

    I wrote the code like the code below and it works, but when I clicked the dropdownlist it only show for Id (sectorid) not the Text (sectorname)

    @Html.DropDownList("sectorid", new SelectList((System.Collections.IEnumerable)ViewData["DBSector"]), "sectorid", "sectorname")

    How to display Text (sectorname) ?

    You only need to exchange the positions of sectorid and sectorname.

    like:

    @Html.DropDownList("sectorid", new SelectList((System.Collections.IEnumerable)ViewData["DBSector"]),"sectorname","sectorid")

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 25, 2019 8:47 AM
  • User-1330485181 posted

    Hi Yuki Tao,

    Thanks for your suggestion. Problem solved.

    Regards,

    Sentoso

    Wednesday, February 27, 2019 8:14 AM