14-11-16, 02:08 PM
عندي مشكلة وهي إني كلما أحاول أن أدخل بيانات الدولة والمحافظة والمدينة تدخل فقط بيانات الدولة أما الباقي تصبح فارغة طبعا أنا استخدمت cascading
كيف أجعل جميع البيانات تضاف في الجدول ولكم جزيل الشكر
هذا كود html في الصفحة طبعا يعمل create item ويضعها في جدول item وهذه البيانات التي لم تدخل معي عدا الدولة فقط هي التي تعمل
وهذا كود jquery
وهذه دالة الحفظ في controller اسمه item
[HttpGet]
public ActionResult Create()
{
List<Category> catlist = db.Categories.ToList();
SelectList sl = new SelectList(catlist.AsEnumerable(), "id", "name");
ViewBag.SelectCategories = sl;
List<Country> countrylist = db.CountryTb.ToList();
SelectList s2 = new SelectList(countrylist.AsEnumerable(), "id", "name");
ViewBag.SelectCountry = s2;
return View();
}
[HttpPost]
public ActionResult Create(Item i)
{
db.Items.Add(i);
db.SaveChanges();
return RedirectToAction("Index");
}
وهذا JsonResult طبعا موجود في كنترول آخر تحت اسم country
public JsonResult GetStatesById(int ID)
{
db.Configuration.ProxyCreationEnabled = false;
return Json(db.StatesTb.Where(p => p.country_id == ID), JsonRequestBehavior.AllowGet);
}
public JsonResult GetCitiesById(int ID)
{
db.Configuration.ProxyCreationEnabled = false;
return Json(db.CityTb.Where(p => p.state_id == ID), JsonRequestBehavior.AllowGet);
}
كيف أجعل جميع البيانات تضاف في الجدول ولكم جزيل الشكر
هذا كود html في الصفحة طبعا يعمل create item ويضعها في جدول item وهذه البيانات التي لم تدخل معي عدا الدولة فقط هي التي تعمل
كود :
<!--Country-->>
<div class="form-group">
@Html.LabelFor(model => model.Country, 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.Country, "", new { @class = "text-danger" })
</div>
</div>
<!--States-->>
<div class="form-group">
@Html.LabelFor(model => model.States, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<select id="statelistdrop" class="form-control" name=""></select>
@Html.ValidationMessageFor(model => model.States, "", new { @class = "text-danger" })
</div>
</div>
<!--City-->>
<div class="form-group">
@Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<select id="citieslistdrop" class="form-control"></select>
@Html.ValidationMessageFor(model => model.City, "", new { @class = "text-danger" })
</div>
</div>
وهذا كود jquery
كود :
<script>
$(function () {
$("#countryId").change(function () {
$.get("/Country/GetStatesById", { ID: $("#countryId").val()}, function (data) {
$("#statelistdrop").empty();
$.each(data, function (index,row) {
$("#statelistdrop").append(" <option value='" + row.state_id + "'>" + row.name+ "</option>")
});
})
});
});
</script>
<script>
$(function () {
$("#statelistdrop").change(function () {
$.get("/Country/GetCitiesById", { ID: $("#statelistdrop").val() }, function (data) {
$("#citieslistdrop").empty();
$.each(data, function (index,row) {
$("#citieslistdrop").append(" <option value='" + row.state_id + "'>" + row.name + "</option>")
});
})
});
});
</script>
وهذه دالة الحفظ في controller اسمه item
[HttpGet]
public ActionResult Create()
{
List<Category> catlist = db.Categories.ToList();
SelectList sl = new SelectList(catlist.AsEnumerable(), "id", "name");
ViewBag.SelectCategories = sl;
List<Country> countrylist = db.CountryTb.ToList();
SelectList s2 = new SelectList(countrylist.AsEnumerable(), "id", "name");
ViewBag.SelectCountry = s2;
return View();
}
[HttpPost]
public ActionResult Create(Item i)
{
db.Items.Add(i);
db.SaveChanges();
return RedirectToAction("Index");
}
وهذا JsonResult طبعا موجود في كنترول آخر تحت اسم country
public JsonResult GetStatesById(int ID)
{
db.Configuration.ProxyCreationEnabled = false;
return Json(db.StatesTb.Where(p => p.country_id == ID), JsonRequestBehavior.AllowGet);
}
public JsonResult GetCitiesById(int ID)
{
db.Configuration.ProxyCreationEnabled = false;
return Json(db.CityTb.Where(p => p.state_id == ID), JsonRequestBehavior.AllowGet);
}