I am new to MVC. So far, I succeeded in binding data from database in a drop down list.

How can I get the selected value and pass it to the Controller to be saved in the database?

Here's what I have done so far:


@Html.DropDownList("State", null, new { @class = "dropdown-toggle col-md-9 form-control" })

CONTOLLER: (this is how bound the data in the dropdown list)

  IEnumerable<SelectListItem> states = db.RefState
            .Select(s => new SelectListItem
                Value = s.ID.ToString(),
                Text = s.Name

        ViewBag.AddressType = types;
        ViewBag.State = states;
        return View();

This is how I save it in the database:

    public ActionResult NewAddress(Address data)
            var addressData = new Address()
                ZipCode = data.ZipCode,
                StreetNo = data.StreetNo,
                StreetName = data.StreetName,
                Unit = data.Unit,
                Additional = data.Additional,
                Town = data.Town,
                City = data.City,
                StateID = ?,  
                Description = data.Description,
                VendorCode = data.VendorCode


            data = null;
            ViewBag.Message = "Address succesfully added";
            return RedirectToAction("DeliveryInformation", "DeliveryDetail");

        return View(data);

Related posts

Recent Viewed