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

  • Question

  • User982203039 posted

    I am getting an error on when I submit on an edit page:
    There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Branch'.

    I am confused on that is going on. Any ideas? Here my code:

    Edit View:

            <div class="form-group">
                @Html.LabelFor(model => model.BRANCH, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model =>model.BRANCH, (IEnumerable<SelectListItem>)ViewBag.Branches, null, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.BRANCH, "", new { @class = "text-danger" })
                </div>
            </div>

    Controller:

    public ActionResult EditDonationRequest(int id)
            {
                var Branches = bd.Branches.Select(c => new {
                    BranchName = c.BranchNum + " - " + c.BranchName
                }).ToList();
    
                var donationrequest = dr.DonationRequests.Find(id);
                if (donationrequest == null)
                {
                    return HttpNotFound();
                }
                ViewBag.Branches = new SelectList(Branches, "BranchName", "BranchName");
                return View(donationrequest);
            }
    
            [HttpPost]
            public ActionResult EditDonationRequest(int id, DonationRequest donationrequest)
            {
     
                try
                {
                    db.Entry(donationrequest).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("DonationRequestDetails", new { id = id });
                }
                catch
                {
                    return View();
                }
            }

    Wednesday, August 21, 2019 9:44 PM

All replies

  • User1520731567 posted

    Hi Baze72,

    [HttpPost]
            public ActionResult EditDonationRequest(int id, DonationRequest donationrequest)
            {
     
                try
                {
                    db.Entry(donationrequest).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("DonationRequestDetails", new { id = id });
                }
                catch
                {
                    return View();
                }
            }

    According to your error and code,I guess you did not assign value to ViewBag.Branches in DonationRequestDetails action,

    So the DonationRequestDetails View has not the ViewBag.Branches correctly set after httppost action.

    I suggest you could add a line ViewBag.Branches definition in DonationRequestDetails action,for example:

    public ActionResult DonationRequestDetails(int id)
    {
    ...
    var Branches = bd.Branches.Select(c => new {
    BranchName = c.BranchNum + " - " + c.BranchName
    }).ToList();

    ViewBag.Branches = new SelectList(Branches, "BranchName", "BranchName");
    ...
    return View();
    }

    Best Regards.

    Yuki Tao

    Thursday, August 22, 2019 5:45 AM
  • User982203039 posted

    Hi Yuji,

    I tried that but still get

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

    I even tried to change the redirect to just go to the index page - same issue. Any other thoughts?

    Thursday, August 22, 2019 1:38 PM
  • User-474980206 posted

    your code is probably throwing an error. try:

            [HttpPost]
            public ActionResult EditDonationRequest(int id, DonationRequest donationrequest)
            {
     
                try
                {
                    db.Entry(donationrequest).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("DonationRequestDetails", new { id = id });
                }
                catch
                {
                    var Branches = bd.Branches.Select(c => new {
                        BranchName = c.BranchNum + " - " + c.BranchName
                    }).ToList();
                    ViewBag.Branches = new SelectList(Branches, "BranchName", "BranchName");
                    return View();
                }
            }

    Thursday, August 22, 2019 2:10 PM