我有这个表名Item,当我编辑表的一行时,它返回State和City下拉列表的空值。但是在数据库中已经添加了但是dropdownlist显示为空我不知道如何检索选定的值 在这里我使用的所有代码
此处为表项目
[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代码,但我认为它只在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; }
}
此处表格状态
[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; }
}
答案 0 :(得分:0)
在@DropDownList
上尝试更改@DropDownListFor(c => c.CountryID, (SelectList)ViewBag.SelectCountry, "select please", new { @class = "form-control" })
Begin窗体将显示更改值选项下拉列表,您的示例CountryID
将发送给您的控制器
答案 1 :(得分:0)
问题可能是因为您可能没有设置正确的导航属性。 您提到 Foriegn键的方式有效,但缺少反向导航。请将以下行添加到相应的类中。
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; }
有关实体框架导航属性和关系
的更多information,请参阅此链接