Here is my opinion on the image 
The code is working fine, but ...
When I submit the form, it sends only the value of the first drop-down list (I checked the received network arguments of the browser), also when I look at the source of the page, it does not show the generated parameters that I generated using the ajax function.
Here is my code
The action that my first dropdownList generates
public ActionResult TwoDropDownList() { HotelContext H = new HotelContext(); ViewBag.DropDownListOne = new SelectList(H.Continent.ToList(), "Id", "Name"); return View(); }
Action returning json from second data dropdownlist
[HttpPost] public JsonResult UpdateCountryDropDownList(int ContinentId) { HotelContext H = new HotelContext(); List<SelectListItem> CountryNames = new List<SelectListItem>(); List<Country> Co = H.Country.Where(x => x.ContinentId == ContinentId).ToList(); Co.ForEach(x => { CountryNames.Add(new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); }); return Json(CountryNames , JsonRequestBehavior.AllowGet); }
My Ajax call
@model Hotel.Models.Continent <script> $(document).ready(function () { $("#Name").change(function () { var ContinentoId = $(this).val(); $.ajax({ type: "POST", dataType: "json", data: { ContinentId: ContinentoId }, url: '@Url.Action("UpdateCountryDropDownList","Home")', success: function (result) { var Country = "<select id='ddlCountry'>"; Country = Country + '<option value="">--Select--</option>'; for (var i = 0; i < result.length; i++) { Country = Country + '<option value=' + result[i].Value + '>' + result[i].Text + '</option>'; } Country = Country + '</select>'; $('#Countries').html(Country); }, error: function (xhr, ajaxOptions, thrownError) { console.log(arguments) } }); }); }) </script>
My view
@using(Html.BeginForm()){ SelectList se = ViewBag.DropDownListOne; @Html.DropDownListFor(x=>x.Name,se,"--Select--") <div id ="Countries"> @Html.DropDownList("ddlCountry",new List<SelectListItem>(),"--Select--") </div> <input type="submit" value="submit" style="margin-top:100px;" /> }
HTTPPost Action
[HttpPost] public string TwoDropDownList(string Name, string ddlCountry) { if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(ddlCountry)) { return ("you must select Both"); } else return ("everything is working fine"); }