1

I have Dropdownlist for country and state when i select country it give me states name according to country but i have to save the selected value in database but i dont know how to do that Please help.

My Controller

public ActionResult Index()
{
    // List<OrbatTable> CountryList = db.OrbatTables.ToList();
    List<OrbatTable> CountryList = db.OrbatTables.DistinctBy(x => x.CityName).ToList();
    ViewBag.CountryList = new SelectList(CountryList, "CityName", "CityName");
    return View();
}

public JsonResult GetStateList(String CityName)
{
    db.Configuration.ProxyCreationEnabled = false;
    List<OrbatTable> StateList = db.OrbatTables.Distinct().Where(x => x.CityName== CityName).Distinct().ToList();
    return Json(StateList.Distinct(), JsonRequestBehavior.AllowGet);
}

My View

<div class="container">
    <div class="form-group">
        @if (ViewBag.CountryList != null)
        {
           @Html.DropDownListFor(model => model.CityName, ViewBag.CountryList as   SelectList , "--Select City", new { @class = "form-control" })
        }
    </div>
    <div class="form-group">
        @Html.DropDownListFor(model => model.UnitName, new SelectList(""), "--Select Unit", new { @class = "form-control" })
    </div>
</div>
Saadi
  • 2,211
  • 4
  • 21
  • 50
  • You're asking how to save a selection from a drop-down to a database in MVC? What do you not understand about the information you found when you searched the web? – jmcilhinney May 03 '18 at 08:41
  • 1
    Possible duplicate of [Html.DropdownListFor selected value not being set](https://stackoverflow.com/questions/19476530/html-dropdownlistfor-selected-value-not-being-set) – Daniel May 03 '18 at 08:47

0 Answers0