1
我在DB中有兩個表。 cascade dropdownlist asp.net
我想創建後that.this城市和相應的區域下拉列表是在我的整個life.i've我的第一個梯級drodown菜單試圖按照一些examples.in的取得動作我:
ViewBag.cities = db.cities.ToList();
ViewBag.areas = db.areas.ToList();
視圖
我:
<div class="editor-field">
@Html.DropDownListFor(model => model.city,new SelectList(ViewBag.cities as
System.Collections.IEnumerable, "city_id", "name"),
"Select City", new { id = "ddlCars" })
@Html.ValidationMessageFor(model => model.city)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.area)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.area, new
SelectList(Enumerable.Empty<SelectListItem>(), "area_id", "name"),
"Select Area", new { id = "ddlModels" })
@Html.ValidationMessageFor(model => model.area)
</div>
我只是複製了一個網站,是
$(document).ready(function() {
$("#ddlCars").change(function() {
var idModel = $(this).val();
$.getJSON("/Post/LoadAreasByCity", { id: idModel },
function (carData) {
var select = $("#ddlModels");
select.empty();
select.append($('<option/>', {
value: 0,
text: "Select Area"
}));
$.each(carData, function (index, itemData) {
select.append($('<option/>', {
value: itemData.Value,
text: itemData.Text
}));
});
});
});
$("#ddlModels").change(function() {
var idColour = $(this).val();
$.getJSON("/Home/LoadColoursByModel", { id: idColour },
function (modelData) {
var select = $("#ddlColours");
select.empty();
select.append($('<option/>', {
value: 0,
text: "Select a Colour"
}));
$.each(modelData, function (index, itemData) {
select.append($('<option/>', {
value: itemData.Value,
text: itemData.Text
}));
});
});
});
});
JS文件
在我的崗位/ LoadAreasByCity我的方法是:
public JsonResult LoadAreasByCity(string id)
{
PostManager pm = new PostManager();
if (id=="") { id = "1"; }
int c_id =(int) Convert.ToInt32(id);
var areaList = pm.GetModels(c_id);
var modelData = areaList.Select(m => new SelectListItem()
{
Text = m.name,
Value = m.area_id.ToString(),
});
return Json(modelData, JsonRequestBehavior.AllowGet);
}
它傳播城市和地區是正確的觀點page.but提交它給人的數據errorin此行
@Html.DropDownListFor(model => model.city,new SelectList(ViewBag.cities as
System.Collections.IEnumerable, "city_id", "name"),"Select City", new { id =
"ddlCars" })
後它說值不能爲空。 參數名稱:項目
最後在我的崗位行動
int c_id = Convert.ToInt32(p.city);
int a_id = Convert.ToInt32(p.area);
area a = db.areas.Single(x=>x.area_id == a_id);
city c = db.cities.Single(s => s.city_id == c_id);
post.city = c.name;
post.area = a.name;
....Save to DB
什麼是在這個問題.....在此先感謝
謝謝....它工作正常... –