【发布时间】:2016-11-29 08:53:09
【问题描述】:
我尝试在 mvc 页面上使用 3 个下拉列表,如果我更改 ddl 1,则 ddl 2 中的值应该会更改,而 ddl 2 的更改应该会更改 ddl 3 中的值。到目前为止,我有这个代码.. .. ddl 1 的值已设置,但如果我更改 ddl 1 中的值,则不会发生任何事情。 ddl 2 没有得到任何值,根本没有触发“public JsonResult GetPapperByTypeId(int typeId)”。
我在这里错过了什么?
主页视图
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "myForm", name = "papper_search" }))
{
@Html.DropDownList("PrintType", ViewData["papperType"] as List<SelectListItem>, new { @class = "form-control" })
<br />
<br />
@Html.DropDownList("Papper", new SelectList(string.Empty, "Value", "Text"), "Please select a paper", new { @class = "form-control" })
<br />
<br />
@Html.DropDownList("PapperType", new SelectList(string.Empty, "Value", "Text"), "Please select a type", new { @class = "form-control" })
}
<script type="text/javascript" src="@Url.Content("~/js/jquery.min.js")"></script>
<script type="text/jscript">
$(function ()
{
$('#PrintType').change(function ()
{
$.getJSON('/Home/GetPapperByTypeId/' + $('#PrintType').val(), function (data)
{
var items = '<option>Select Papper</option>';
$.each(data, function (i, printtype)
{
items += "<option value='" + printtype.Value + "'>" + printtype.Text + "</option>";
});
$('#Papper').html(items);
});
});
$('#Papper').change(function ()
{
$.getJSON('/Home/Citylist/' + $('#Papper').val(), function (data)
{
var items = '<option>Select PapperType</option>';
$.each(data, function (i, pappertype)
{
items += "<option value='" + pappertype.Value + "'>" + pappertype.Text + "</option>";
});
$('#PapperType').html(items);
});
});
});
</script>
家庭控制器
public ActionResult Index()
{
var li = new List<SelectListItem>
{
new SelectListItem {Text = "Select", Value = "0"},
new SelectListItem {Text = "Plain paper", Value = "1"},
new SelectListItem {Text = "Heavy paper", Value = "2"},
};
ViewData["papperType"] = li;
}
//Action result for ajax call
public JsonResult GetPapperByTypeId(int typeId)
{
var objallPappers = GetAllPappers().Where(m => m.TypeId == typeId).ToList();
var obgpapper = new SelectList(objallPappers, "Id", "Name", 0);
return Json(obgpapper, JsonRequestBehavior.AllowGet);
}
public List<Papper> GetAllPappers()
{
var objPapper = new List<Papper>
{
new Papper {Id = 1, TypeId = 1, Name = "papper-1"},
new Papper {Id = 2, TypeId = 2, Name = "papper-1"},
new Papper {Id = 3, TypeId = 4, Name = "papper-1"},
new Papper {Id = 4, TypeId = 1, Name = "papper-2"},
new Papper {Id = 5, TypeId = 1, Name = "papper-3"},
new Papper {Id = 6, TypeId = 4, Name = "papper-2"}
};
return objPapper;
}
【问题讨论】:
-
您是否遇到任何错误。签入控制台窗口
-
我现在看到我收到了这个错误...“TypeError: $ is not a function”在我的 jScript 中..
-
这意味着你没有包含
jquery或者它没有正确加载或者存在命名冲突 -
感谢斯蒂芬提供的信息。我现在开始工作了:)
标签: asp.net-mvc cascadingdropdown