ASP.NET MVC级联下拉列表使用AJAX返回未定义的返回

发布于 2025-02-13 06:03:37 字数 2028 浏览 1 评论 0原文

我试图为类和主题列出级联下拉列表时,当我从下拉列表中选择一个类时,主题下拉列表显示了响应类中的主题。 但是,控制器中返回的主题价值是(未定义)

这是我的观点

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文