【问题标题】:Modal View Reload Content (Bootstrap MVC ASP.NET)模态视图重新加载内容 (Bootstrap MVC ASP.NET)
【发布时间】:2016-08-04 17:05:24
【问题描述】:

Aloha,我在 ASP.NET MVC4 应用程序中使用 Bootstrap v3.3.6 来使用模态视图显示弹出窗口。

我的模态示例:

<div class="modal-contents modal-view content-wrapper">
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    </div>
    <div class="modal-body content-wrapper">
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $('#approve-btn').click(function () {
            $('#modal-container').modal('hide');
        });
    });
</script>

要加载模态,我使用 Html.ActionLink 之类的

@Html.ActionLink("SomeText", "SomeAction", "SomeController", null , new { @class="modal-link"})

在我的 _Layout 页面中触发我的脚本:

<script type="text/javascript">
     $(function () {
         // Initialize modal dialog
         // attach modal-container bootstrap attributes to links with .modal-link class.
         // when a link is clicked with these attributes, bootstrap will display the href content in a modal dialog.
         $('body').on('click', '.modal-link', function (e) {
             e.preventDefault();
             $(this).attr('data-target', '#modal-container');
             $(this).attr('data-toggle', 'modal');
             console.log(this);
         });
         // Attach listener to .modal-close-btn's so that when the button is pressed the modal dialog disappears
         $('body').on('click', '.modal-close-btn', function () {
             $('#modal-container').modal('hide');
         });
         //clear modal cache, so that new content can be loaded

         $('.modal').on('hidden.bs.modal', function () {
             $(this).removeData('bs.modal');
         });

         $('#CancelModal').on('click', function () {
             return false;
         });
     });
</script>

最后是我的 _Layout 中的占位符

<div id="modal-container" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-content modal-view">
    </div>
</div>

这一切都是根据这个设计的:http://www.codeproject.com/Tips/826002/Bootstrap-Modal-Dialog-Loading-Content-from-MVC-Pa 并且工作得很好。直到我已经在同一个视图中打开了一个模式后才尝试打开一个模式。然后我会发现模态包含与以前相同的数据。
在尝试了来自:
Reload content in modal (twitter bootstrap)
的所有建议后 我觉得问题出在#modal-container 的重新填充上。 链接由脚本以正确的方式完成,正如我在 chrome 中调试时所说的那样。
如果我重新加载整个页面,模式视图会接受新内容。 但是由于我使用标签(也依赖于 Bootstrap)重新加载不是一个选项,因为我会丢失当前的标签选择。

我对网络开发非常陌生,所以如果你有一些想法会很棒。

【问题讨论】:

  • 在什么情况下,您尝试打开具有相同模态但已打开不同内容的模态?这是一个有点不寻常的情况。
  • 我描述得很糟糕。我的意思是关闭模式并从我的父视图中使用另一个链接(和数据)重新打开它。我正在显示一个 Webgrid 并尝试显示不同的详细信息。我已经尝试了您提到的问题的大多数解决方案。但我会找一些备用的。

标签: javascript asp.net asp.net-mvc twitter-bootstrap


【解决方案1】:

你可以使用

ModelState.Clear();

用于清除控制器中的模型状态。

希望对你有帮助。

【讨论】:

  • 不,这是模特
  • 他的问题与ModelState无关。他的问题是关于自举模态的行为。
猜你喜欢
  • 2017-08-16
  • 2016-04-24
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多