locked
Binding a drop down List with Model throwing an error RRS feed

  • Question

  • User351619809 posted

    I am trying to bind a drop down list to a model using entity framework and I am getting an error saying:

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

    Below is my Model class:

     public partial class bookAssignment_new
            {
                public int ID { get; set; }
                public Nullable<int> Book { get; set; }
                public Nullable<int> OfficeID { get; set; }
                public string Office { get; set; }
                public IEnumerable<SelectListItem> OfficeName { get; set; }
          }
    

    Below is my controller:

    public ActionResult Index()
            {
                var model = new bookAssignment_new();
                using (BookAssignmentEntities db = new BookAssignmentEntities())
                {
                    var dbData = db.OfficeNames.ToList();
                    model.OfficeName = GetSelectListItems(dbData);
                }
                return View();
            }
        
        
            private IEnumerable<SelectListItem> GetSelectListItems(IEnumerable<OfficeName> elements)
                {
                    var selectList = new List<SelectListItem>();
                    foreach (var element in elements)
                    {
                        selectList.Add(new SelectListItem
                        {
                            Value = element.OfficeID.ToString(),
                            Text = element.Office.ToString()
                        });
                    }
                    return selectList;
                }
    

    I can see that right data is returned by GetSelectListItems().

    My view has the following code:

     <div class="form-group">
            @Html.DropDownList("OfficeName", Model.OfficeName)
           
        </div>
    

    I am getting above error in the view. My OfficeName table is like so:

    enter image description here

    below is the screenshot of the error:

    enter image description here

    any help will be appreciated.

    Saturday, September 19, 2020 7:40 PM

Answers

  • User475983607 posted

    The Index action does not pass the model to the view.  

    public ActionResult Index()
    {
        var model = new bookAssignment_new();
        using (BookAssignmentEntities db = new BookAssignmentEntities())
        {
            var dbData = db.OfficeNames.ToList();
            model.OfficeName = GetSelectListItems(dbData);
        }
        return View(model);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 20, 2020 9:50 AM
  • User1686398519 posted

    Hi anjaliagarwal5, 

    Did you add Key Attribute to the Model called OfficeName?

    In addition, you need to make the action named Index return a View with model.

    I used your code for testing and there is no error.

    DropDownList has different writing methods, you can try other writing methods,  you can refer to the specific code below.

    Model

        public partial class OfficeName
        {
            [Key]
            public int OfficeID { get; set; }
            public string Office { get; set; }
        }

    Controller

            public ActionResult Index()
            {
                var model = new bookAssignment_new();
                using (DailyMVCDemoContext db = new DailyMVCDemoContext())
                {
                    var dbData = db.OfficeNames.ToList();
                    model.OfficeName = GetSelectListItems(dbData);
                }
                return View(model);
            }

    Index

    @model DailyMVCDemo.Models.bookAssignment_new
    @Html.DropDownListFor(m => m.OfficeName, Model.OfficeName)
    @Html.DropDownListFor(m => m.OfficeName, new SelectList(Model.OfficeName, "Value", "Text"),"Select")
    @Html.DropDownList("OfficeName", Model.OfficeName)
    @Html.DropDownList("OfficeName", new SelectList(Model.OfficeName, "Value", "Text"), "Select")

    Here is the result.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 21, 2020 3:21 AM

All replies

  • User475983607 posted

    The Index action does not pass the model to the view.  

    public ActionResult Index()
    {
        var model = new bookAssignment_new();
        using (BookAssignmentEntities db = new BookAssignmentEntities())
        {
            var dbData = db.OfficeNames.ToList();
            model.OfficeName = GetSelectListItems(dbData);
        }
        return View(model);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 20, 2020 9:50 AM
  • User351619809 posted

    I made the changes that you mentioned above. I am still getting the same error.

    Sunday, September 20, 2020 5:59 PM
  • User1686398519 posted

    Hi anjaliagarwal5, 

    Did you add Key Attribute to the Model called OfficeName?

    In addition, you need to make the action named Index return a View with model.

    I used your code for testing and there is no error.

    DropDownList has different writing methods, you can try other writing methods,  you can refer to the specific code below.

    Model

        public partial class OfficeName
        {
            [Key]
            public int OfficeID { get; set; }
            public string Office { get; set; }
        }

    Controller

            public ActionResult Index()
            {
                var model = new bookAssignment_new();
                using (DailyMVCDemoContext db = new DailyMVCDemoContext())
                {
                    var dbData = db.OfficeNames.ToList();
                    model.OfficeName = GetSelectListItems(dbData);
                }
                return View(model);
            }

    Index

    @model DailyMVCDemo.Models.bookAssignment_new
    @Html.DropDownListFor(m => m.OfficeName, Model.OfficeName)
    @Html.DropDownListFor(m => m.OfficeName, new SelectList(Model.OfficeName, "Value", "Text"),"Select")
    @Html.DropDownList("OfficeName", Model.OfficeName)
    @Html.DropDownList("OfficeName", new SelectList(Model.OfficeName, "Value", "Text"), "Select")

    Here is the result.

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 21, 2020 3:21 AM