Ruben van Luther
Ruben van Luther

Reputation: 1

How to fix 'There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'UserID_TO'.' in asp.net mvc

Every time submit, the following error appears "There is no View Data item of type 'IEnumerable ' that has the key 'UserID_TO'." However, the data that was submitted earlier successfully entered the database. Even though it successfully entered, this error persisted.

I've tried with DropDownList or DropDownListFor.

My Controller


        [HttpGet]
        public ActionResult AddOrEdit()
        {

            InputCP inputCP = new InputCP();
            ViewBag.UserID_TO = new SelectList(db.Users, "ID", "Name");
            ViewBag.UserID_CC = new SelectList(db.Users, "ID", "Name");
            ViewBag.UserID_Sales = new SelectList(db.Users, "ID", "Name");
            return View(inputCP);
        }

        [HttpPost]
        public ActionResult AddOrEdit(InputCP inputCP)
        {
            try
            {
                inputCP.Created = DateTime.Now;
                inputCP.LastModified = DateTime.Now;
                db.InputCPs.Add(inputCP);
                db.SaveChanges();

                ViewBag.Message = "Success";
                return View();

            }
            catch (Exception ex)
            {
                ViewBag.Exception = ex;
                ViewBag.ErrorMessage = "An error occured, please check your data input and try again";
            }
            return View("Error");

        }

My Model

    using System.Collections.Generic;
    using System.Web.Mvc;

    public partial class InputCP
    {
        public int ID { get; set; }
        public Nullable<System.Guid> UserID_TO { get; set; }
        public Nullable<System.Guid> UserID_CC { get; set; }
        public Nullable<System.Guid> UserID_Sales { get; set; }
        public string Product { get; set; }
        public string DrawingSpect { get; set; }
        public string Spec { get; set; }
        public string Questioner { get; set; }
        public string COGS { get; set; }
        public Nullable<int> CustomerID { get; set; }
        public string PartNumber { get; set; }
        public Nullable<int> CurrencyCode { get; set; }
        public Nullable<decimal> Price { get; set; }
        public Nullable<decimal> LeadTime { get; set; }
        public Nullable<decimal> Validity { get; set; }
        public string Warranty { get; set; }
        public string Notes { get; set; }
        public Nullable<System.DateTime> Created { get; set; }
        public Nullable<System.DateTime> LastModified { get; set; }

        public IEnumerable<SelectListItem> User { get; set; }
    }

My View

            @Html.LabelFor(model => model.UserID_TO, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(Model => Model.UserID_TO, (IEnumerable<SelectListItem>)ViewBag.UserID_TO, "Select User", htmlAttributes: new { @class = "form-control full-width mt-1" })
                @Html.ValidationMessageFor(model => model.UserID_TO, "", new { @class = "text-danger validation-error" })
            </div>
        </div>

I expect the results if successful, that is, to the default home view

Upvotes: 0

Views: 136

Answers (1)

Mannan Bahelim
Mannan Bahelim

Reputation: 1365

The problem is because of post back happens on submit button click. So while posting data back to same page add your ViewBag before returning View().

because MVC looks through your ViewData for something named UserID_TO and throws an error if there's no object in ViewData with that name.

        [HttpPost]
        public ActionResult AddOrEdit(InputCP inputCP)
        {

            ViewBag.UserID_TO = new SelectList(db.Users, "ID", "Name");
            ViewBag.UserID_CC = new SelectList(db.Users, "ID", "Name");
            ViewBag.UserID_Sales = new SelectList(db.Users, "ID", "Name");

            try
            {
                inputCP.Created = DateTime.Now;
                inputCP.LastModified = DateTime.Now;
                db.InputCPs.Add(inputCP);
                db.SaveChanges();

                ViewBag.Message = "Success";
                return View();

            }
            catch (Exception ex)
            {
                ViewBag.Exception = ex;
                ViewBag.ErrorMessage = "An error occured, please check your data input and try again";
            }
            return View("Error");

        }

Upvotes: 0

Related Questions