Jquery 当我单击“编辑”按钮时,MVC正在从数据库检索所选值到级联onload
我有这个表名项,当我编辑表中的一行时,它会为State和City dropdownlist返回空值。但在数据库中已添加但dropdownlist显示为空我不知道如何检索所选值 这里是我使用的所有代码 这里是表格项目Jquery 当我单击“编辑”按钮时,MVC正在从数据库检索所选值到级联onload,jquery,asp.net-mvc,entity-framework,ef-code-first,Jquery,Asp.net Mvc,Entity Framework,Ef Code First,我有这个表名项,当我编辑表中的一行时,它会为State和City dropdownlist返回空值。但在数据库中已添加但dropdownlist显示为空我不知道如何检索所选值 这里是我使用的所有代码 这里是表格项目 [Table("Item")] public class Item { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int id { get; set; } [R
[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");
}
[HttpGet]
公共行动结果编辑(内部id)
{
List countrylist=db.CountryTb.ToList();
SelectList s2=新的SelectList(countrylist.AsEnumerable(),“id”,“name”);
ViewBag.SelectCountry=s2;
Item I=db.Items.Single(x=>x.id==id);
返回视图(I);
}
[HttpPost]
[ActionName(“Edite”)]
公共行动结果编辑发布(内部id)
{
Item I=db.Items.Single(x=>x.id==id);
UpdateModel(I,null,null,新字符串[]{“id”});
db.SaveChanges();
返回操作(“索引”);
}
这是我的jQuery代码,但我认为它只在一次更改时起作用,我希望它在加载时也起作用,以检索所选的值
<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>
$(函数(){
$(“#CountryId”).change(函数(){
$.get(“/Country/getstatesbyd”,{ID:$(“#CountryId”).val()},函数(数据){
$(“#StateId”).empty();
$.each(数据、函数(索引、行){
$(“#StateId”).append(“+row.name+”)
});
})
});
});
$(函数(){
$(“#StateId”).change(函数(){
$.get(“/Country/GetCitiesById”,{ID:$(“#StateId”).val()},函数(数据){
$(“#CityId”).empty();
$.each(数据、函数(索引、行){
$(“#CityId”).append(“+row.name+”)
});
})
});
});
这里是我的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>
@LabelFor(model=>model.CountryId,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.DropDownList(“CountryId”,(SelectList)ViewBag.SelectCountry,“请选择”,新建{@class=“form control”})
@Html.ValidationMessageFor(model=>model.CountryId,“,new{@class=“text danger”})
@LabelFor(model=>model.StateId,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownList(“StateId”,新选择列表(Enumerable.Empty(),“state_id”,“name”),“--Select--”,新{@class=“form control”})
@Html.ValidationMessageFor(model=>model.StateId,“,new{@class=“text danger”})
@LabelFor(model=>model.CityId,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownList(“CityId”,new SelectList(Enumerable.Empty(),“id”,“name”),“--Select--”,new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.CityId,“,new{@class=“text danger”})
这里是表格类别
[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; }
}
[表格(“类别”)]
公共类类别
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int id{get;set;}
[必需]
公共字符串名称{get;set;}
公共虚拟ICollection项{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; }
}
[表格(“国家”)]
公营国家
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int id{get;set;}
公共字符串sortname{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection状态{get;set;}
公共虚拟ICollection项{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; }
}
[表格(“状态”)]
公共阶级国家
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int状态_id{get;set;}
公共字符串名称{get;set;}
public int country_id{get;set;}
[外国钥匙(“国家/地区识别号”)]
公共虚拟国家{get;set;}
公共虚拟ICollection城市{get;set;}
公共虚拟ICollection项{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; }
}
[表格(“城市”)]
公营城市
{
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int id{get;set;}
公共字符串名称{get;set;}
公共int状态_id{get;set;}
[外国钥匙(“州id”)]
公共虚拟状态状态{get;set;}
公共虚拟ICollection项{get;set;}
}
在@DropDownListFor(c=>c.CountryID,(SelectList)ViewBag.SelectCountry,“请选择”,新建{@class=“表单控制”})上尝试更改@DropDownList
。
Begin表单将看到更改值选项下拉列表,您的示例
CountryID
将发送到控制器问题可能是因为您没有设置正确的导航属性。
[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");
}
您引用外键的方式有效,但缺少反向导航。请将下面的行添加到相应的类中
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; }
公共虚拟ICollection类别{get;set;}
公共虚拟ICollection国家{get;set;}
公共虚拟ICollection状态{get;set;}
公共虚拟ICollection城市{get;set;}
有关实体框架导航属性和关系的更多信息,请参阅此链接有关实现级联下拉列表(特别是控制器中的代码)的信息。您可以显示您的类别类吗?我添加了类别,我之前已经在相应的类上添加了它!我想一定是来自jquery?!!请用类别或国家/地区类别更新您的帖子。您使用此选项修复了吗?尚未修复。我认为jquery可能需要编辑?您的console.log显示了什么?