MVC将值从子模态传递到父模态

时间:2018-10-10 01:43:42

标签: javascript jquery asp.net-mvc modal-dialog

美好的一天!

我有一个Index View,当用户单击表中的按钮时,将弹出具有不同Html.EditorFor的模式。前两个Html.EditorFor将根据表中的选定值进行填充,而其他Html.EditorFor在聚焦时将显示另一个包含数据表的模式。

因此,基本上,这是一个 在模态中包含模态的视图

问题是,当我从子模式中单击“选择”按钮时,它应该填充“父模式”中的Html.EditorFor,但是发生的是它将我带到“父模式”剃刀视图和以前填充的数据现在已删除。我想要的是只使用子模态中的值填充父模态Html.EditorFor

This是该代码的示例视频。

父模块(DropAppliPopup)

<div class="modal-body">

        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()

            <div class="panel panel-primary">

                <div class="panel-heading"><h4>PETITIONER INFORMATION</h4></div>
                <div class="panel-body">

                    <div class="form-group">
                        @Html.LabelFor(model => model.petitioner_name, htmlAttributes: new { @class = "control-label" })
                        @Html.EditorFor(model => model.petitioner_name, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @Value = @ViewBag.Petname, placeholder = @Html.DisplayNameFor(m => m.petitioner_name), @id = "petname" } })
                        @Html.ValidationMessageFor(model => model.petitioner_name, "", new { @class = "text-danger" })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.petitioner_address, htmlAttributes: new { @class = "control-label" })
                        @Html.EditorFor(model => model.petitioner_address, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @Value = @ViewBag.Petaddress, placeholder = @Html.DisplayNameFor(m => m.petitioner_address), @id = "petaddress" } })
                        @Html.ValidationMessageFor(model => model.petitioner_address, "", new { @class = "text-danger" })
                    </div>

            <div class="panel panel-primary">
                <div class="panel-heading"><h4>PETITIONEE INFORMATION</h4></div>
                <div class="panel-body">

                    <div class="form-group">
                        @Html.LabelFor(model => model.operator_name, htmlAttributes: new { @class = "control-label" })
                        @Html.EditorFor(model => model.operator_name, new { htmlAttributes = new { @class = "form-control", @Value = @ViewBag.OName, placeholder = @Html.DisplayNameFor(m => m.operator_name), @id = "petname", onclick = "ClickTextbox()" } })
                        @Html.ValidationMessageFor(model => model.operator_name, "", new { @class = "text-danger" })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.operator_address, htmlAttributes: new { @class = "control-label" })
                        @Html.EditorFor(model => model.operator_address, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", placeholder = @Html.DisplayNameFor(m => m.operator_address), @id = "petaddress" } })
                        @Html.ValidationMessageFor(model => model.operator_address, "", new { @class = "text-danger" })
                    </div> 

        }

    </div>
</div>

儿童模型

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" onclick = "CloseModal()" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    Modal title
                </h4>
            </div>
            <div class="modal-body">
                @{ Html.RenderAction("PayerList2");  }
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" onclick = "CloseModal()">
                    Close
                </button>
            </div>
        </div>
    </div>
</div>

PayerList2部分视图

  @foreach (var item in Model)
        {
            <tr>
                <td>
                    <a href="@Url.Action("DropAppliPopup","Dropped",new { opname=@item.operator_name, opmotor=@item.motor_no });">Select</a>
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.franchise_no)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.motor_no)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.motor_id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.operator_id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.operator_name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.for_year)
                </td>

            </tr>
        }

CONTROLLER

public ActionResult DropAppliPopup(string Objid, string Petname, string Petaddress, string opname, string opmotor)
{
   ViewBag.Year = new SelectList(db.tbl_Year.OrderByDescending(x => x.year), "year", "year");
   ViewBag.DrpReason = new SelectList(db.tbl_DroppingReason, "id", "Reason");
   ViewBag.Petname = Petname;
   ViewBag.Petaddress = Petaddress;
   ViewBag.Petid = Objid;
   ViewBag.OName = opname;
   ViewBag.OMotor = opmotor;

   return PartialView("DropAppliPopup");
}

脚本

    function ClickTextbox() {
        $('#myModal').modal('show');
        $("#myModal").modal({ backdrop: "static" });
        $("#myModal").modal({ keyboard: false });
        $('#myModal').on('shown.bs.modal', function () {
            $('#txtUserName').focus();
        });
    };

0 个答案:

没有答案