在模态中加载模型只工作一次

本文关键字:一次 工作 模态 加载 模型 | 更新日期: 2023-09-27 18:02:43

我有这个恼人的问题,我一直试图解决过去的几个晚上。不过到目前为止还没有运气。我有一个视图与模态和部分视图,被渲染成模态。我根据在单击表中的按钮时传递给控制器的参数,用数据填充partialview。第一个很好,button调用controller -> controller将模型传递给partialview ->模态打开,正确的数据在字段中。

当我单击另一条记录上的按钮时,将打开模态并显示旧数据。控制器没有被再次调用,因此数据不会刷新。问题是,如果我用shift + f5刷新页面,它确实又工作了一次。所以这似乎是一个缓存问题。

我已经尝试过了—禁用我的控制器和方法中的缓存-调用javascript方法从我的模态中删除数据-使用其他情态动词

索引视图

<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
    <div class="col-lg-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>List of registrations</h5>
                <div class="ibox-tools">
                    @Html.ActionLink("Create New", "Create", null, new { @class = "btn btn-primary btn-xs" })
                </div>
            </div>
            <div class="ibox-content">
                <table class="table table-striped" id="datatable">
                    <thead>
                        <tr>
                            <th>
                                @Html.DisplayNameFor(model => model.Date)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.Car.Kenteken)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.DepartureLocation)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.ArrivalLocation)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.Distance)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.Allowance)
                            </th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model)
                        {
                            <tr>
                                <td>
                                    @Html.DisplayFor(modelItem => item.Date)
                                </td>
                                <td>
                                    @Html.DisplayFor(modelItem => item.Car.Kenteken)
                                </td>
                                <td>
                                    @Html.DisplayFor(modelItem => item.DepartureLocation)
                                </td>
                                <td>
                                    @Html.DisplayFor(modelItem => item.ArrivalLocation)
                                </td>
                                <td>
                                    @Html.DisplayFor(modelItem => item.Distance)
                                </td>
                                <td>
                                    @Html.DisplayFor(modelItem => item.Allowance)
                                </td>
                                <td>
                                    @Html.ActionLink("Edit", "Edit", new { id = item.CarId }, new { @class = "btn btn-white btn-sm" })
                                    @Html.ActionLink("Delete", "Delete", new { id = item.CarId }, new { @class = "btn btn-white btn-sm" })
                                    @Html.ActionLink("Copy", "CopyTripRegistrationModal", "TripRegistration", new { registrationId = item.RegistrationID }, new { @class = "img-btn-addnote modal-link btn btn-white btn-sm" })
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div id="modal-container" class="modal fade" tabindex="-1" role="dialog">
<a href="#close" title="Close" class="modal-close-btn">X</a>
<div class="modal-content">
    <div class="modal-body"></div>
</div>

Javascript

<script>
$(function () {
    $('body').on('click', '.modal-link', function (e) {
        e.preventDefault();
        $(this).attr('data-target', '#modal-container');
        $(this).attr('data-toggle', 'modal');
    });
    $('body').on('click', '.modal-close-btn', function () {
        $('#modal-container').modal('hide');
    });
    debugger;
    $("#modal-container").on("hidden.bs.modal", function () {
        $(".modal-body").removeData();
    });
});

<

部分视图/strong>

<div class="modal-body">
        @using (Html.BeginForm("Create", "TripRegistration"))
        {
            @Html.AntiForgeryToken()
            <div class="form-horizontal">
                @Html.ValidationSummary(true)
                @Html.HiddenFor(model => model.CarId)
                <div class="form-group">
                    @Html.LabelFor(model => model.Date, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => Model.Date, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Date)
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.DepartureLocation, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.DepartureLocation)
                        @Html.ValidationMessageFor(model => model.DepartureLocation)
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.DepartureZipcode, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.DepartureZipcode)
                        @Html.ValidationMessageFor(model => model.DepartureZipcode)
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.ArrivalLocation, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.ArrivalLocation)
                        @Html.ValidationMessageFor(model => model.ArrivalLocation)
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.ArrivalZipcode, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.ArrivalZipcode)
                        @Html.ValidationMessageFor(model => model.ArrivalZipcode)
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.Distance, new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Distance)
                        @Html.ValidationMessageFor(model => model.Distance)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <button type="button" class="btn btn-white" id="cancel">Close</button>
                        <button type="submit" class="btn btn-primary">Copy registration</button>
                    </div>
                </div>
            </div>
        }
    </div>

控制器动作

        [NoCache]
    public PartialViewResult CopyTripRegistrationModal(int registrationId)
    {
        var tripRegistration = _tripRegistrationService.getTripRegistrationById(registrationId);
        var tripRegistrationVM = AutoMapper.Mapper.Map<tblTripRegistration, TripRegistrationViewModel>(tripRegistration);
        return PartialView("_CopyTripRegistration", tripRegistrationVM);        
    }

我已经添加了"[OutputCache(Duration = 0)]"到控制器的顶部。

我希望有人能帮助我!

在模态中加载模型只工作一次

可以显式地进行ajax调用,并将其响应设置为模态主体。

$(function () {
     $('body').on('click', '.modal-link', function (e) {
         e.preventDefault();
         $("#modal-container").remove();
         $.get($(this).attr("href"), function (data) {
                 $('<div id="modal-container" class="modal fade">
                        <div class="modal-content" id="modalbody">' 
                                               + data + '</div></div>').modal();
         });
    });
});