我有这个表的名称Item,当我编辑表的一行时,它返回空值的状态和城市下拉列表。但在数据库中已经添加,但下拉列表显示空我不知道如何检索选定的值这里是我用过的所有代码
here Table Item
[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; }
}
code
[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代码但我认为它只能onchange工作我希望它也能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; }
}
here table States
[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; }
}
here table City
[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; }
}
尝试在@DropDownListFor(c => c.CountryID, (SelectList)ViewBag.SelectCountry, "select please", new { @class = "form-control" })
上更改@DropDownList
Begin表单将看到更改值选项下拉框,您的示例CountryID
将发送到控制器
问题可能是因为您可能没有设置适当的导航属性。您引用外键的方式是有效的,但缺少反向导航。请将下列行添加到相应的类别中。
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Country> Countries { get; set; }
public virtual ICollection<States> States { get; set; }
public virtual ICollection<City> Cities { get; set; }
有关实体框架导航属性和关系的更多信息,请参阅此链接