代码优先,数据没有使用级联下拉列表插入数据库

时间:2014-04-24 06:57:37

标签: c# asp.net-mvc-4 cascading

在我的[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>

1 个答案:

答案 0 :(得分:1)

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

适用于DepartmentsDepartmentIdSubjectsSubjectId。他们需要在Post中匹配,粘合剂将附加正确的值