ASP.NET MVC级联下拉列表使用AJAX返回未定义的返回
我试图为类和主题列出级联下拉列表时,当我从下拉列表中选择一个类时,主题下拉列表显示了响应类中的主题。 但是,控制器中返回的主题价值是(未定义)
这是我的观点
<div class="form-group">
@Html.LabelFor(model => model.PDFClass, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.PDFClass, ViewBag.ClassName as SelectList, "Please Select", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PDFClass, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PDFSubject, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.PDFSubject, new SelectList(" "), "Please Select", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PDFSubject, "", new { @class = "text-danger" })
</div>
</div>
这是Ajax
<script>
$(document).ready(function () {
$("#PDFClass").change(function () {
$.get("/Home/GetStateList", { Class: $("#PDFClass").val() }, function (data) {
$("#PDFSubject").empty();
$.each(data, function (index, row) {
$("#PDFSubject").append("<option value = '" + row.Class + "'>" + row.SubjectName + "</option>")
debugger;
});
});
})
});
</script>
这是该方法获得类&amp; amp;受试者的关系表与Ajax
public JsonResult GetStateList(string Class)
{
var List1 = context.ClassesWithSubjects_.ToList().Where(i => i.ClassName == Class);
return Json(List1, JsonRequestBehavior.AllowGet);
}
Im trying to make a cascading dropdown list for Classes and Subjects, when I select a Class from the dropdown list the subject Dropdown shows subjects that are in the responding Class.
But the subject value that is returned in the controller is (Undefined)
Here is my view
<div class="form-group">
@Html.LabelFor(model => model.PDFClass, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.PDFClass, ViewBag.ClassName as SelectList, "Please Select", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PDFClass, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PDFSubject, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.PDFSubject, new SelectList(" "), "Please Select", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PDFSubject, "", new { @class = "text-danger" })
</div>
</div>
here is the Ajax
<script>
$(document).ready(function () {
$("#PDFClass").change(function () {
$.get("/Home/GetStateList", { Class: $("#PDFClass").val() }, function (data) {
$("#PDFSubject").empty();
$.each(data, function (index, row) {
$("#PDFSubject").append("<option value = '" + row.Class + "'>" + row.SubjectName + "</option>")
debugger;
});
});
})
});
</script>
Here is the method that gets a list of Classes&Subjects relation Table to the Ajax
public JsonResult GetStateList(string Class)
{
var List1 = context.ClassesWithSubjects_.ToList().Where(i => i.ClassName == Class);
return Json(List1, JsonRequestBehavior.AllowGet);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论