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

Analyst_SQL 3,551 Reputation points
2023-03-26T11:52:22.46+00:00

When i am passing data from view to controller then below error is coming

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


                                <div class="form-group">
                                    @Html.LabelFor(model => model.Design_ID, "Select Designation Level", htmlAttributes: new { @class = "control-label col-md-6" })
                                    <div class="col-md-10">
                                        @Html.DropDownList("designid", null, "Select Designation", htmlAttributes: new { @class = "form-control", @id = "select2-1" })
                                        @Html.ValidationMessageFor(model => model.Design_ID, "", new { @class = "text-danger" })
                                    </div>
                                </div>

  public ActionResult NewCompany()
        {
            var model = new CompanyMV();

            var deptid = 0;
            ViewBag.Dept_ID = new SelectList(DB.tbl_department.Where(bt => bt.Dept_ID > deptid ), "Dept_ID", "Dept_Name", "0");

            var designid = 0;
 
      

            ViewBag.designid = new SelectList(DB.tbl_Designation.Where(bt => bt.Design_ID > designid), "Design_ID", "Design_Name", "0");



            return View(model);
        }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,282 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,141 Reputation points
    2023-03-26T20:28:41.6666667+00:00

    i did not understand ,what you want to make me understand was working fine,can you create example for me plz.or where i am making mistake

    namespace MvcBasic.Controllers
    {
        public class tbl_Designation
        {
            public int Design_ID { get; set; }
            public string Design_Name { get; set; }
        }
        public class BasicFormsController : Controller
        {
            private List<tbl_Designation> tbl_Designations;
            public BasicFormsController()
            {
                tbl_Designations = new List<tbl_Designation>()
                {
                    new tbl_Designation()
                    {
                        Design_ID = 1,
                        Design_Name = "Hello"
                    },
                    new tbl_Designation()
                    {
                        Design_ID = 2,
                        Design_Name = "World"
                    },
                    new tbl_Designation()
                    {
                        Design_ID = 3,
                        Design_Name = "Foo"
                    },
                    new tbl_Designation()
                    {
                        Design_ID = 4,
                        Design_Name = "Bar"
                    },
                };
            }
            // GET: BasicForms
            [HttpGet]
            public ActionResult Index()
            {
                ViewBag.Design_ID = new SelectList(tbl_Designations.AsEnumerable(), "Design_ID", "Design_Name");
                return View();
            }
    
            [HttpPost]
            public ActionResult Index(tbl_Designation model)
            {
                ViewBag.Design_ID = new SelectList(tbl_Designations.AsEnumerable(), "Design_ID", "Design_Name");
                return View(model);
            }
    
        }
    }
    
    @model MvcBasic.Controllers.tbl_Designation
    
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Index</h2>
    
    @using (Html.BeginForm()) 
    {
        @Html.AntiForgeryToken()
        
        <div class="form-horizontal">
            <h4>ViewModel</h4>
            <hr />
            <div class="form-group">
                <div class="col-md-10">
                    <div class="checkbox">
                        @Html.DropDownList("Design_ID", null, "Select Designation", htmlAttributes: new { @class = "form-control", @id = "select2-1" })
                    </div>
                </div>
            </div>
    
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Submit" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
    
    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. AgaveJoe 26,141 Reputation points
    2023-03-26T15:22:40.49+00:00

    There is a misunderstanding of how the DropDownList overload works. The DropDownList is named "designid". Therefore, DropDownList HTML helper is looking for a key in the ViewBag that matches the DropDownList named "designid" which does not exist.

    @Html.DropDownList("designid", null, "Select Designation", htmlAttributes: new { @class = "form-control", @id = "select2-1" })
    

    Below is the ViewBag.designid assignment which does not have a value option named designid. The option value is named Design_ID.

    ViewBag.designid = new SelectList(DB.tbl_Designation.Where(bt => bt.Design_ID > designid), "Design_ID", "Design_Name", "0");
    

    On a side note, unless you have records in the tbl_Designation table Design_ID equal to or less than zero there is no need for the Where.

    Reference documentation

    DropDownList Class

    Most beginning level tutorials cover this concept.

    Using the DropDownList Helper with ASP.NET MVC


  2. hafsa ashraf 80 Reputation points
    2023-03-26T20:12:52.1733333+00:00

    The error message suggests that the key "designid" is not found in the ViewData dictionary that is passed from the controller to the view.

    One possible solution is to ensure that the name of the DropDownList matches the key in the ViewData dictionary. In this case, you should change the DropDownList code to:
    @Html.DropDownList("Design_ID", null, "Select Designation", htmlAttributes: new { @class = "form-control", @id = "select2-1" })


  3. QiYou-MSFT 4,306 Reputation points Microsoft Vendor
    2023-03-27T05:55:32.6533333+00:00

    Hi @akhter hussain

    @Html.DropDownList("designid", null, "Select Designation", htmlAttributes: new { @class = "form-control", @id = "select2-1" })

    The "designid" in this stands for List<SelectListItem> datalist. "Select Designation" It represents the initial value of the dropdownlist.

    I can give an example.

    code:

    public ActionResult Index()
            {
                
                ViewData["designid"] = GetList();
                return View();
            }
            private List<SelectListItem> GetList()
            {
                List<SelectListItem> datalist = new List<SelectListItem>();
                for (int i = 0; i < 24; i++)
                {
                    if (i < 10)
                    {
                        datalist.Add(new SelectListItem
                        {
                            Text = "0" + i.ToString(),
                            Value = "0" + i.ToString()
                        });
                    }
                    else
                    {
                        datalist.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() });
                    }
                }
                return datalist;
            }
    
    @Html.DropDownList("designid", null, "Select Designation", htmlAttributes: new { @class = "form-control", @id = "select2-1" })
    

    output:

    Test1

    Best regards,
    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.