-1
我試圖有一對級聯下拉,縮小基於選定狀態的城市列表。到目前爲止,我有這樣的: 查看:Asp.Net MVC與Razor級聯DropdownList
<div class="form-group">
@Html.LabelFor(model => model.CollLocation, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="col-md-6">
@Html.DropDownList("stateCol", null, htmlAttributes: new { @class = "form-control" }, optionLabel: "Select a state")
@Html.ValidationMessageFor(model => model.CollLocation, "", new { @class = "text-danger" })
</div>
<div class="col-md-6">
@Html.DropDownList("CollLocation", null, htmlAttributes: new { @class = "form-control" }, optionLabel: "Select a city")
@Html.ValidationMessageFor(model => model.CollLocation, "", new { @class = "text-danger" })
</div>
而這個控制器:
// GET: Coll/Create
public ActionResult Create()
{
var stateColl = db.ZipCodes.OrderBy(c => c.state).Select(c => c.state).Distinct();
var cityCol = db.ZipCodes.Select(C => C.primary_city).Distinct();
ViewBag.stateCol = new SelectList(stateColl);
ViewBag.ArRecID = new SelectList(db.ArRecs, "ArRecID", "ArZipID");
ViewBag.CollLocation = new SelectList(cityCol);
return View();
}
// POST: Coll/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CollID,ArRecID,CollName,CollDescr,CollValue,CollOwner,CollLocation,DateCreated,ModBy,ModDate,CreatedBy")] Collateral collateral)
{
if (ModelState.IsValid)
{
db.Coll.Add(coll);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ArRecID = new SelectList(db.ArRecs, "ArRecID", "ArZipID", coll.ArRecID);
ViewBag.CollLocation = new SelectList(db.ZipCodes, "zip", "primary_city", coll.CollLocation).Distinct();
return View(collateral);
}
在我的拉鍊碼模型我有拉鍊,primary_city和狀態。在CollLocation中,我希望只能看到stateCol下拉列表中選中狀態的引用。這兩個下拉菜單對我來說都很有用,但是,它們不能一起工作。我嘗試了其他的教程和答案,但他們只是讓我更抓撓我的頭。任何幫助將不勝感激。
推薦你學習代碼[此DotNetFiddle](https://dotnetfiddle.net/1bPZym) - 您需要ajax才能正常工作,但您的代碼還存在其他多種問題 –
非常感謝。這是一個很好的小提琴。它幫助並清除了我的很多問題 – TomBB