首先代码,数据未使用级联下拉列表插入数据库

本文关键字:级联 下拉列表 插入 数据库 未使用 数据 代码 | 更新日期: 2023-09-27 17:59:40

在我的创建[ Http post ]方法中,除了级联下拉项外,所有数据都入。我有部门,学科和部门模型。一个部门可以有多个科目,一个科目可以有多个部分。添加jQuery提交部分后,显示表单无法提交!然后它返回到索引!这是我来自部分控制器的代码:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Section section)
{
    if (ModelState.IsValid)
    {
        db.Sections.Add(section);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    ViewBag.DepartmentId = new SelectList(db.Departments, "DepartmentId", "Name", section.DepartmentId);
    ViewBag.SubjectId = new SelectList(db.Subjects, "SubjectId", "SubjectName", section.SubjectId);
    return View(section);
}

这是我在Secion视图下的create.chtml:

@model MvcBCS.Models.Section
@{
  ViewBag.Title = "Create Section";
}
@section scripts {
<script type="text/javascript">
    $(function() {
        $.getJSON("/Section/Departments/List", function(data) {
            var items = "<option> Show Department List </option>";
            $.each(data, function(i, department) {
                items += "<option value='" + department.Value + "'>" + department.Text + "</option>";
            });
            $("#Departments").html(items);
        });
        $("#Departments").change(function() {
            $.getJSON("/Section/Subjects/List/" + $("#Departments > option:selected").attr("value"), function(data) {
                var items = "<option> Show Subject List </option>";
                $.each(data, function(i, subject) {
                    items += "<option value='" + subject.Value + "'>" + subject.Text + "</option>";
                });
                $("#Subjects").html(items);
            });
        });

    });
    $(function() {
        $('#submit').on("click", function () {
            var form = $(this).parent("form");
            $.ajax({
                type: "POST",
                url: form.attr('action'),
                data: form.serialize()
            })
                .success(function() {
                    alert("Your form has been submitted");
                })
                .error(function() {
                    alert("Your form has not been submitted");
                });
            return false;
        });
    });

</script>
}
<h2>Create Section</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Section</legend>
        <label for="Departments">Departments</label>
           <select id="Departments" name="Departments"></select>
           <label for="Subjects">Subjects</label>
           <select id="Subjects" name="Subjects"></select>
           <label for="Sections">Section</label>
           <input id="Sections" name="Sections" type="text" />
           <p>
                <input type="submit" id="submit" value="Create" />
            </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

首先代码,数据未使用级联下拉列表插入数据库

我认为您需要更改两个下拉列表中有关您部分中属性的name/Id

为了Departments DepartmentIdSubjects SubjectId.它们需要匹配,以便在您的Post活页夹将附加正确的值