【问题标题】:Show Partial view in Bootstrap modal在 Bootstrap 模式中显示部分视图
【发布时间】:2018-05-11 06:36:07
【问题描述】:

我正在创建 MVC 应用程序并使用 Bootstrap Modal 让用户有机会检查输入数据,并使用 Ajax 将这些数据发送到控制器。模态窗口:

<div class="modal" tabindex="-1" role="dialog" id="myModal" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Check input data</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div> 
                @using (Ajax.BeginForm("SelectReservationDateTime", new AjaxOptions { UpdateTargetId = "divChangeRegion" }))

                {
                        <div class="modal-body">
                            <table class="table">
                                <tr>
                                    <td>Category name: </td>
                                    <td><b>@Model.CategoryName </b></td>
                                </tr>
                                <tr>
                                    <td>Date: </td>
                                    <td><label id="SelectedDateTimeLabel"></label></td>
                                </tr>
                                <tr>
                                    <td>Client name: </td>
                                    <td><input type="text" name="ClientName" id="ClientName" value="@Model.ClientName" /></td>
                                </tr>
                                <tr>
                                    <td>Client code: </td>
                                    <td><input type="text" name="ClientCode" id="ClientCode" value="@Model.ClientCode" /></td>
                                </tr>                               
                            </table>

                        </div>

                        <input type="hidden" name="CategoryId" id="CategoryId" value="@Model.CategoryId" />
                        <input type="hidden" name="SelectedDateTime" id="SelectedDateTime" />


                        <div class="modal-footer" id="divChangeRegion">
                            <input type="submit" class="btn btn-info" value="OK" data-backdrop="static">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                        </div>
                    }         

            </div>
        </div>
    </div>

在按下提交按钮后,我执行控制器的方法,将输入的数据保存在数据库中并发送回部分视图,我想在那些模态中输入 控制器代码:

public ActionResult SelectReservationDateTime(ReservationTimeModel PartialViewModel)
        {
            ...

            return PartialView(newModel);
        }

部分查看代码:

@model EQueue.Data.Ticket

<div class="row">
    <div class="col-md-4 offset-md-4">
        <table>
            <tr>
                <td class="border" id="printThis">
                    You get ticket №<b>@Model.CodeTicket</b><br />
                    Category Name <br />
                    <b>"@Model.TicketCategory.NameCategory"</b><br />
                </td>
            </tr>
            <tr>
                <td><input type="button" class="btn btn-primary text-center" value="Print ticket" onclick="printTicket('printThis')" data-dismiss="modal"></td>
                <td><input type="button">
            </tr>
        </table>
    </div>
</div>

<script>
    function printTicket(partForPrint)
    {
        var printContents = document.getElementById(partForPrint).innerHTML;
        var originalContents = document.body.innerHTML;

        document.body.innerHTML = printContents;

        window.print();

        document.body.innerHTML = originalContents;
    }


</script>

但是我得到了新窗口而不是在现有的模态中创建部分视图,有人可以帮助我吗?

【问题讨论】:

  • 你在哪里展示你的局部视图?
  • 我希望在 Modal 中显示部分视图而不是
  • 在下面查看我的答案。在 modal-footer 部分内部,但在输入和按钮周围有一个 div,并创建另一个具有 id 的 div。当您从 ajax 返回成功时,将带有按钮的 div 和输入设置为 display:none;并用调用结果填充另一个 div。

标签: html css ajax asp.net-mvc


【解决方案1】:

首先我会改变这一行:

@using (Ajax.BeginForm("SelectReservationDateTime", new AjaxOptions { UpdateTargetId = "divChangeRegion" }))

收件人:

@using(Html.BeginForm(null, null, FormMethod.Post, new {id = "whatever"}))

然后有一些 jquery 使用表单的 id 来检查它是否已提交。一旦有了,您应该对您的返回部分视图的方法进行 ajax 调用。要实现局部视图,您需要使用 id 指定一个 div,然后在成功时使用这行代码将局部视图放入该 div。

$("id of the div").html(result);

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签