我有這個表名稱項目,當我編輯表格的一行時,它會爲狀態和城市下拉列表返回空值。但在數據庫中已添加,但下拉列表顯示空,我不知道如何檢索選定值 ,在這裏,我是用當我單擊編輯按鈕時,MVC從數據庫檢索選定值到級聯onload
這裏表項目項目控制器
[Table("Item")]
public class Item
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
[Required]
public string name { get; set; }
public string info { get; set; }
public decimal price { get; set; }
public int CatId { get; set; }
public int? CountryId { get; set; }
public int? StateId { get; set; }
public int? CityId { get; set; }
[ForeignKey("CatId")]
public virtual Category Category { get; set; }
[ForeignKey("CountryId")]
public virtual Country Country { get; set; }
[ForeignKey("StateId")]
public virtual States States { get; set; }
[ForeignKey("CityId")]
public virtual City City { get; set; }
}
代碼編輯我所有的代碼
[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");
}
,這是我的jQuery代碼,但我認爲這是唯一的工作電平變化我想onload事件也它的工作在檢索選定值
<script>
$(function() {
$("#CountryId").change(function() {
$.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>
<script>
$(function() {
$("#StateId").change(function() {
$.get("/Country/GetCitiesById", { ID: $("#StateId").val() }, function (data) {
$("#CityId").empty();
$.each(data, function (index, row) {
$("#CityId").append(" <option value='" + row.id + "'>" + row.name + "</option>")
});
})
});
});
</script>
這裏我的HTML代碼
<!--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>
這裏表類別
[Table("Category")]
public class Category
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
[Required]
public string name { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
這裏表國家
[Table("countries")]
public class Country
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string sortname { get; set; }
public string name { get; set; }
public virtual ICollection<States> states { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
這裏表美國
[Table("states")]
public class States
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int state_id { get; set; }
public string name { get; set; }
public int country_id { get; set; }
[ForeignKey("country_id")]
public virtual Country countries { get; set; }
public virtual ICollection<City> cities { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
這裏表市
[Table("cities")]
public class City
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string name { get; set; }
public int state_id { get; set; }
[ForeignKey("state_id")]
public virtual States states { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
參見[本DotNetFiddle](https://dotnetfiddle.net/1bPZym)用於實現級聯dropdownlists(特別是在代碼控制器) –
你能顯示你的類別類嗎? – Aravind
我添加了類 –