Mvc从级联下拉列表中保存项目

时间:2018-09-26 17:04:42

标签: c# asp.net-mvc cascadingdropdown

这是我的控制器。

public class DokuzasController : Controller
{
    schoolEntities se = new schoolEntities();

    public ActionResult Index()
    {
        IEnumerable<DersViewModel> leclist;
        HttpResponseMessage responselec = GlobalVariables.LecturesClient.GetAsync("dokuzas").Result;
        leclist = responselec.Content.ReadAsAsync<IEnumerable<DersViewModel>>().Result;
        return View(leclist);
    }

    public ActionResult AddOrEdit()
    {
        List<ders> dersList = se.ders.ToList();
        ViewBag.dersList = new SelectList(dersList, "DersID", "Ders1");
        return View();
    }

    public JsonResult GetDersList(int DersID)
    {
        se.Configuration.ProxyCreationEnabled = false;
        List<saat> saatList = se.saat.Where(x => x.DersID == DersID).ToList();
        return Json(saatList, JsonRequestBehavior.AllowGet);

    }
    [HttpPost]
    public ActionResult AddOrEdit(DersViewModel lec)
    {
        if (lec.LectureId == 0)
        {
            HttpResponseMessage response = GlobalVariables.LecturesClient.PostAsJsonAsync("dokuzas", lec).Result;
            TempData["SuccessMessage"] = "Kaydedildi.";
        }
        else
        {
            HttpResponseMessage response = GlobalVariables.LecturesClient.PutAsJsonAsync("dokuzas/" + lec.LectureId, lec).Result;
            TempData["SuccessMessage"] = "Güncellendi.";
        }
        return RedirectToAction("Index");
    }
}

这是AddOrEdit的一部分。

@model Mvc.Models.DersViewModel
<div class="container">
<div class="form-group">
    @if (ViewBag.dersList != null)
    {
        @Html.DropDownListFor(model => model.DersID, ViewBag.dersList as SelectList, "--Lecture--", new { @class = "form-control" })
    }
</div>

<div class="form-group">
    @Html.DropDownListFor(model => model.SaatID, new SelectList(" "), "--Time--", new { @class = "form-control" })
</div>
</div>
@using (Html.BeginForm())
{
<div class="form-group">
    <input type="submit" value="Kaydet" class="btn button" />
    <input type="reset" value="Sil" class="btn button" />
</div>
}
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function () {
    $("#DersID").change(function () {
        $.get("/Dokuzas/GetDersList", { DersID: $("#DersID").val() }, 
function (data) {
            $("#SaatID").empty();
            $.each(data, function (index, row) {
                $("#SaatID").append("<option value='" + row.SaatID + "'>" + row.Saat1 + "</option>");
            });
        });
    });
});
</script>

我为讲座和时间创建了一个级联的下拉列表,但无法将其保存到表格中。当我选择项目并选择提交按钮时,我只能向表提交null。它没有保存我从下拉列表中选择的内容。如何从级联下拉列表中保存到表中?

1 个答案:

答案 0 :(得分:0)

您的下拉列表不在表单内,因此它们不包含在帖子中。请尝试以下代码进行查看。

@model Mvc.Models.DersViewModel
@using (Html.BeginForm())
{
<div class="container">
<div class="form-group">
    @if (ViewBag.dersList != null)
    {
        @Html.DropDownListFor(model => model.DersID, ViewBag.dersList as SelectList, "--Lecture--", new { @class = "form-control" })
    }
</div>

<div class="form-group">
    @Html.DropDownListFor(model => model.SaatID, new SelectList(" "), "--Time--", new { @class = "form-control" })
</div>
</div>

<div class="form-group">
    <input type="submit" value="Kaydet" class="btn button" />
    <input type="reset" value="Sil" class="btn button" />
</div>
}
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function () {
    $("#DersID").change(function () {
        $.get("/Dokuzas/GetDersList", { DersID: $("#DersID").val() }, 
function (data) {
            $("#SaatID").empty();
            $.each(data, function (index, row) {
                $("#SaatID").append("<option value='" + row.SaatID + "'>" + row.Saat1 + "</option>");
            });
        });
    });
});
</script>