[RESOLVED]Drop box is not returning primary key

I am using Visual Studio Express 2013, C#, Entity Framework 6, ASP.Net MVC 6, on a Windows 8 - 64 bit computer.

Drop box is not returning primary key in drop down, record is saved in database with a zero value. Should be the primary key of the selected row in the drop box.

Data model is EF 6.0

Controller Code

// GET: VehicleOrders/Create
        public ActionResult Create()
        {
            var data = from p in db.Vehicles
                       select p.VehicleName;

            SelectList list = new SelectList(data);
            ViewBag.VechicleNames = list;

            return View();
        }

        // POST: VehicleOrders/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "VechicleOrderId,VechicleId,CustomerName,DateRequested,BuildDate")] VehicleOrder vehicleOrder)
        {
            if (ModelState.IsValid)
            {
                db.VehicleOrders.Add(vehicleOrder);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(vehicleOrder);
        }

View code

 <div class="form-horizontal">
        <h4>Vehicle Order</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.VechicleId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @*@Html.EditorFor(model => model.VechicleId, new { htmlAttributes = new { @class = "form-control" } })*@
                @Html.DropDownList("VehicleId", ViewBag.VechicleNames as SelectList, "", new { id = "VehicleId" })
                @Html.ValidationMessage("VehicleId", "*")
                @Html.ValidationMessageFor(model => model.VechicleId, "", new { @class = "text-danger" })
            </div>
        </div>

Your SelectList needs to contain the primary key value as the Value property for each list item while the vehicle name is the Text property.

I added value and text to the SelectList but I still get a value of zero. The drop down shows the three rows from EF Vehicles table as it should, but if I select any one of the three, the value is always zero. What am I missing in my code to get the selected
value to save to db?

I’m going around in circles trying to get this simple task completed.

controller code

      // GET: VehicleOrders/Create
        public ActionResult Create()
        {
            var data = from p in db.Vehicles
                       select new
                       {
                           VehicleId = p.VehicleId,
                           VehicleName = p.VehicleName
                       };
                        

            //SelectList list = new SelectList(data);
            ViewBag.ListOfVehicles = new SelectList(data, "VehicleId", "VehicleName");

            return View();
        }

        // POST: VehicleOrders/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "VechicleOrderId,VechicleId,CustomerName,DateRequested,BuildDate")] VehicleOrder vehicleOrder)
        {
            if (ModelState.IsValid)
            {
                db.VehicleOrders.Add(vehicleOrder);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(vehicleOrder);
        }

View code

 <div class="form-horizontal">
        <h4>Vehicle Order</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.VehicleId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @*@Html.EditorFor(model => model.VehicleId, new { htmlAttributes = new { @class = "form-control" } })*@
                @Html.DropDownList("ListOfVehicles", ViewBag.VehicleNames as SelectList, "", ViewBag.VehicleId as SelectList)
                @*@Html.DropDownListFor(
                    x => x.VehicleId,
             new SelectList(model => model.VehicleId, "VehicleId", "VehicleName")
                    )*@
                @Html.ValidationMessage("VehicleName", "*")
                @Html.ValidationMessageFor(model => model.VehicleId, "", new { @class = "text-danger" })
            </div>
        </div>

Please change dropdown list in the view as given below

@Html.DropDownList("VechicleId",
(
SelectList)ViewBag.ListOfVehicles)

Still get zero for the value. What else do I need to change?

Controller code

      // GET: VehicleOrders/Create
        public ActionResult Create()
        {
            var data = from p in db.Vehicles
                       select new
                       {
                           VehicleId = p.VehicleId,
                           VehicleName = p.VehicleName
                       };
                        

            //SelectList list = new SelectList(data);
            ViewBag.ListOfVehicles = new SelectList(data, "VehicleId", "VehicleName");

            return View();
        }

        // POST: VehicleOrders/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "VechicleOrderId,VechicleId,CustomerName,DateRequested,BuildDate")] VehicleOrder vehicleOrder)
        {
            if (ModelState.IsValid)
            {
                db.VehicleOrders.Add(vehicleOrder);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(vehicleOrder);
        }

View code

I corrected the misspelling of vehicle through the solution. Text values are shown in drop down list but the value of the selected row is not saved to database. Could the problem be code missing in the controller when ActionResult Create that save to the
database?

        <div class="form-group">
            <div class="col-md-10">
                @Html.DropDownList("VechicleId", (SelectList)ViewBag.ListOfVehicles)
            </div>
        </div>

Looks spelling of VehicleId is wrong in bind parameters. Please change it as VehicleId and make sure property name in model is VehicleId. Also please check value of VehicleId property in vehicleOrder object when page is post backed.

Change in controller

// POST: VehicleOrders/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "VehicleOrderId,VehicleId ,CustomerName,DateRequested,BuildDate")] VehicleOrder vehicleOrder)
        {
            if (ModelState.IsValid)
            {
                db.VehicleOrders.Add(vehicleOrder);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(vehicleOrder);
        }

View

<div class="form-group">
            <div class="col-md-10">
                @Html.DropDownList("VehicleId", (SelectList)ViewBag.ListOfVehicles)
            </div>
        </div>

Leave a Reply