Retrieving selected value from database to Cascading
short i want Retrieving previous selected value from database to Cascading when i click edit buttoni used this jquery
_________________________________________________________________________________________________
<script>
$(function () {
$("#countryId").on('change', bindStates);
bindStates();
});
function bindStates(){
$.get("/Country/GetStatesById", { ID: $("#countryId").val() }, function (data) {
$("#StateId").empty();
$.each(data, function (index, row) {
$("#StateId").append(" <option value='" + row.state_id + "'>" + row.name + "</option>")
});
})
}
</script>
_________________________________________________________________________________________________
and this my edit button
_________________________________________________________________________________________________
[HttpGet]
public ActionResult Edite(int id)
{
List<Country> countrylist = db.CountryTb.ToList();
SelectList s2 = new SelectList(countrylist.AsEnumerable(), "id", "name");
ViewBag.SelectCountry = s2;
Item I = db.Items.Single(x => x.id == id);
return View(I);
}
[HttpPost]
[ActionName("Edite")]
public ActionResult Edite_post(int id)
{
Item I = db.Items.Single(x => x.id == id);
UpdateModel(I, null, null, new string[] { "id" });
db.SaveChanges();
return RedirectToAction("Index");
}
_________________________________________________________________________________________________
here my cshtml code
_________________________________________________________________________________________________
<!--Country-->
<div class="form-group">
@Html.LabelFor(model => model.CountryId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("countryId", (SelectList)ViewBag.SelectCountry, "select please", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CountryId, "", new { @class = "text-danger" })
</div>
</div>
<!--States-->
<div class="form-group">
@Html.LabelFor(model => model.StateId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("StateId", new SelectList(Enumerable.Empty<SelectListItem>(), "state_id", "name"), "-- Select --", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.StateId, "", new { @class = "text-danger" })
</div>
</div>
<!--City-->
<div class="form-group">
@Html.LabelFor(model => model.CityId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CityId", new SelectList(Enumerable.Empty<SelectListItem>(), "id", "name"), "-- Select --", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CityId, "", new { @class = "text-danger" })
</div>
</div>
_________________________________________________________________________________________________
so how can i fix that