我的看法是
<div id="Countryy">
<div class="editor-label">
@Html.LabelFor(model => model.Country, "Country")
</div>
<div class="editor-field">
@Html.DropDownList("Country", String.Empty)
@Html.ValidationMessageFor(model => model.Country)
</div>
</div>
<div id="Statess">
<div class="editor-label">
@Html.LabelFor(model => model.State, "State")
</div>
<div class="editor-field">
@Html.DropDownList("State", String.Empty)
@Html.ValidationMessageFor(model => model.State)
</div>
</div>
<div id="Cityy">
<div class="editor-label">
@Html.LabelFor(model => model.City, "City")
</div>
<div class="editor-field">
@Html.DropDownList("City", String.Empty)
@Html.ValidationMessageFor(model => model.City)
</div>
</div
我的控制器
public ActionResult Edit(int id)
{
Student student = db.Students.Single(s => s.ID == id);
ViewBag.Country = new SelectList(db.Couns, "ID", "CountryName", student.Country);
ViewBag.State = new SelectList(db.States.Where(d => d.CountryID.Equals(student.Country)), "StateID", "StateName", student.State);
ViewBag.City = new SelectList(db.Cities.Where(x => x.StateID.Equals(student.State)), "CityID", "CityName", student.City);
return View(student);
}
块引用
我的问题是如何级联国家、州和城市下拉列表。当我想编辑数据时会生成此视图。从 db 中保存的数据被检索并绑定到控件,但是当用户更改国家下拉列表的值而不是状态时下拉菜单也应该根据它填充。我在数据库中有国家、州、城市 3 个不同的表,其中包含所需的 pk 和 fk