我在数据库上有2个表tblcustomer和tblphone。我在tblphone中有两个属性(Brand和Model)。我正在使用mvc5创建新客户。我在视图中有2个Dropdownlist,并且我想在Dropdown1(Brand)更改时更改Dropdown2(模型)值。 我已经实现了此代码,但是在按下Submit按钮dropdown2时没有返回通过JSON方法发送的Phone_Fk字段。 请检查代码并更正错误
我认为我在Dropdown或Script查询中都犯了一些错误。
我的观点
@Html.Label("Brand", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Com, new SelectList(ViewBag.Brand),"--Select Brand--")
@Html.ValidationMessageFor(model => model.Com,"", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Phone_Fk, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Phone_Fk, new SelectList(" "), "--Select Model--")
@Html.ValidationMessageFor(model => model.Phone_Fk, "", new { @class = "text-danger" })
</div>
</div>
脚本
<script src="~/scripts/jquery-1.10.2.js"></script>
<script >
$(document).ready(function () {
$("#Com").change(function () {
$.get("/Customer/getmodelList", { Com: $("#Com").val() }, function (data) {
$("#Phone_Fk").empty();
$.each(data, function (index, row) {
$("#Phone_Fk").append("<option value='" + row.Phone_Fk + "'>" + row.model_name + '</option>')
});
});
})
});
</script>
控制器
public JsonResult getmodelList(string Com)
{
db.Configuration.ProxyCreationEnabled = false;
var list = db.Phonedetails.Where(x => x.Com_nam == Com).Select(x => new { x.Phoneid, x.model_name }).ToList();
return Json(list, JsonRequestBehavior.AllowGet);
}
// POST: Customer/Create
[HttpPost]
public ActionResult Create(myappCustomer p)
{
try
{
if (ModelState.IsValid)
{
d.createcus(p);
return RedirectToAction("Index");
}
我想要在dropdown2中选择的模型的ID,但是ID即将为0。