下拉列表,用于不将数据保存在编辑视图中

时间:2018-09-13 18:05:30

标签: javascript c# asp.net-mvc razor

我有问题,因为dropdownlistfor级联没有在视图中保存/显示数据,但是我可以将数据添加到数据库中并且可以。但是当我要编辑数据时,我需要从头开始进行编辑。

编辑视图https://i.stack.imgur.com/YBsMi.png 数据库https://i.stack.imgur.com/nLEht.png

视图中

@Html.DropDownListFor(m => m.IDTypPojazdu, new SelectList(""), "-- Wybierz --", new { @class = "form-control" })
@Html.DropDownListFor(m => m.IDNapedu, new SelectList(""), "-- Wybierz --", new { @class = "form-control" })

控制器

    public ActionResult GetTypList(int IDKategorie)
    {
        WypozyczalniaEntities db = new WypozyczalniaEntities();

        List<TypPojazdu> TypList = db.TypPojazdu.Where(x => x.IDKategorie == IDKategorie).ToList();
        ViewBag.StateOptions = new SelectList(TypList, "IDTypPojazdu", "Nazwa");
        return PartialView("TypPartial");
    }
    public ActionResult GetNapedList(int IDKategoriee)
    {
        WypozyczalniaEntities db = new WypozyczalniaEntities();
        List<Naped> NapedList = db.Naped.Where(x => x.IDKategorie == IDKategoriee).ToList();
        ViewBag.StateOptionss = new SelectList(NapedList, "IDNapedu", "Nazwa");
        return PartialView("NapedPartial");

部分视图

<option value="">-- Wybierz --</option>
@if (ViewBag.StateOptions != null)
{

    foreach (var item in ViewBag.StateOptions)
    {

        <option value="@item.Value">@item.Text </option>

    }

}

<option value="">-- Wybierz --</option>
@if (ViewBag.StateOptionss != null)
{

    foreach (var item in ViewBag.StateOptionss)
    {

        <option value="@item.Value">@item.Text </option>

    }

}

编辑视图脚本

<script>

    $(document).ready(function () {

        $("#IDKategorie").change(function () {

            var IDKategorie = $(this).val();
            debugger
            $.ajax({

                type: "Post",
                url: "/Pojazd/GetTypList?IDKategorie=" + IDKategorie,
                contentType: "html",
                success: function (response) {
                    debugger
                    $("#IDTypPojazdu").empty();
                    $("#IDTypPojazdu").append(response);

                }

            })

        })

    })

</script>
<script>

    $(document).ready(function () {

        $("#IDKategorie").change(function () {

            var IDKategorie = $(this).val();
            debugger
            $.ajax({

                type: "Post",
                url: "/Pojazd/GetNapedList?IDKategoriee=" + IDKategorie,
                contentType: "html",
                success: function (response) {
                    debugger
                    $("#IDNapedu").empty();
                    $("#IDNapedu").append(response);

                }

            })

        })

    })

</script>

0 个答案:

没有答案