Asp.net模态编辑部分视图不保持下拉列表值

时间:2017-05-05 16:08:16

标签: ajax asp.net-mvc

当模式弹出打开时,最初为下拉列表选择的值已更改为列表中的第一个项目。如何保留最初选择的值?

控制器:

    public ActionResult AddEditExam(int exam_id)
    {
        exam exam = new exam();

        ViewBag.credential_id = new SelectList(db.credentials, "credential_id", "credential_code", exam.credential_id);
        ViewBag.credential_status_id = new SelectList(db.credential_status, "credentials_status_id", "credentials_status_description", exam.credential_status_id);

        if (exam_id > 0)
        {
            exam ex = db.exams.SingleOrDefault(x => x.exam_id == exam_id);
            exam.exam_id = ex.exam_id;
            exam.credential_id = ex.credential_id;
            exam.credential_status = ex.credential_status;
            exam.exam_desc = ex.exam_desc;
            exam.exam_last_update = ex.exam_last_update;
            exam.exam_passing_score = ex.exam_passing_score;
            exam.exam_time = ex.exam_time;          
        }
        return PartialView("AddEdit", exam);
    }

部分视图:缩小到下拉列表

 @Html.LabelFor(model => model.credential_id, "credential_id", htmlAttributes: new { @class = "control-label col-md-2" })

        @Html.DropDownList("credential_id", null, htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.credential_id, "", new { @class = "text-danger" })




    @Html.LabelFor(model => model.credential_status_id, "credential_status_id", htmlAttributes: new { @class = "control-label col-md-2" })

        @Html.DropDownList("credential_status_id", null, htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.credential_status_id, "", new { @class = "text-danger" })


<script>
$(document).ready(function () {

    $("#btnSubmit1").click(function () {
        $("#loaderDiv").show();
        var myModelBody1 = $("#myForm").serialize();

        if (!$("#myForm").valid()) {
            return false;
        }
        $.ajax({

            type: "POST",
            url: "@Url.Action("index", "exams")",
            data: myModelBody1,
            success: function () {
                $("#loaderDiv").hide();
                $('#myModalAddEdit').modal('hide');
                window.location.href = "@Url.Action("index", "exams")";

            }
        });
    });
});

原始视图:模态

<div class="modal fade" id="myModalAddEdit">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <a href="#" class="close" data-dismiss="modal">&times;</a>
            <h3 class="modal-title"><strong style="color:#ff9c21">EDIT </strong> Exam</h3>

        </div>
        <div class="modal-body" id="myModelBody1">


        </div>
        <div class="modal-footer">
            <a href="#" id="btnSubmit1" class="btn btn-warning" value="Update">Update</a>
            <a href="#" class="btn btn-default" data-dismiss="modal">Close</a>
        </div>
    </div>
</div>

原始视图:模态脚本

 var AddEditExam = function (exam_id) {

    var exam_id = exam_id;

    var url = '@Url.Action("AddEditExam", "exams")?exam_id=' + exam_id;
    //var url = "http://localhost/NAHP/exams/AddEditExam?exam_id="+exam_id;
    $("#myModelBody1").load(url,
        function() {
            $("#myModalAddEdit").modal('show');
        });

}

0 个答案:

没有答案