在Bootstrap模式中显示局部视图

时间:2018-05-10 12:31:59

标签: html css ajax asp.net-mvc

我正在创建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中创建局部视图,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

首先我会改变这一行:

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

要:

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

然后有一些jquery使用表单的id来检查它是否已被提交。一旦它有你应该激活ajax调用你的方法返回一个partialview。要实现partialview,你需要使用id指定div所在的div,然后在成功时使用这行代码将partialview放入div中。

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