【问题标题】:asp.net mvc bootsrap 4 modal partialviewasp.net mvc bootstrap 4 模态局部视图
【发布时间】:2018-07-05 22:40:36
【问题描述】:

我正在尝试从 bootstrap 3.3.7 更新到 4.0.0,但是带有部分视图的模态不想工作,我不断得到一个空的模态内容而不是部分视图,这里有我的代码使用引导程序 3.3.7

bootstratp 3.3.7 代码https://github.com/emiliano84/testModal/tree/master/WebApplication1 引导 4.0.0 代码 https://github.com/emiliano84/testModal/tree/bootsrap4/WebApplication1

_Layout.cshtml

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - WebApplication1</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>

    <div class="container body-content">
        @RenderBody()
    </div>

    @Html.Partial("_Modal")

    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>

    @RenderSection("Scripts", required: false)

</body>
</html>

__Modal.cshtml

  <div aria-hidden="true" aria-labelledby="modal-action-label" role="dialog" tabindex="-1" id="modal-action-id" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
        </div>
    </div>
</div>

首页/Index.cshtml

 <a id="deleteModal" data-toggle="modal" data-target="#modal-action-id" class="btn btn-danger" asp-action="Modal">
    <i class="glyphicon glyphicon-trash"></i>  Delete
</a>

HomeController.cs

  public IActionResult Modal()
        {
            return PartialView("_DeleteCategory");
        }

首页/_DeleteCategory.cshtml

<form asp-action="" role="form">
    <div class="modal-body form-horizontal">
        Do you want to delete?
    </div>
</form>

点击按钮后的bootsrap 3.7 html输出

<html><head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home Page - WebApplication1</title>
    <link rel="stylesheet" href="/lib/bootstrap/dist/css/bootstrap.css">
    <link rel="stylesheet" href="/css/site.css">
</head>
<body class="modal-open">

    <div class="container body-content">


<a id="deleteModal" data-toggle="modal" data-target="#modal-action-id" class="btn btn-danger" href="/Home/Modal">
    <i class="glyphicon glyphicon-trash"></i>  Delete
</a>
    </div>

    <div aria-hidden="true" aria-labelledby="modal-action-label" role="dialog" tabindex="-1" id="modal-action-id" class="modal fade in" style="display: block;">
    <div class="modal-dialog">
        <div class="modal-content"><form role="form" action="/" method="post">
    <div class="modal-body form-horizontal">
        Do you want to delete?
    </div>
<input name="__RequestVerificationToken" value="CfDJ8KH27-v3I3xDokVa0ArDzjvcs251yDWEn8uK5vM6nFLVSh-l1byQUcPqFqYlR-naInUYVtOGq28Ib186Up7s2ftB5t7krZ1Ix43Agaohw3H0Fq9SbU2wdkdNS93kS-EUTnlmy6Rs3Pu1N4-efa1KO4g" type="hidden"></form>

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

    <script src="/lib/jquery/dist/jquery.js"></script>
    <script src="/lib/bootstrap/dist/js/bootstrap.js"></script>
    <script src="/js/site.js?v=ji3-IxbEzYWjzzLCGkF1KDjrT2jLbbrSYXw-AhMPNIA"></script>

<div class="modal-backdrop fade in"></div></body></html>

点击按钮后的Bootstrap 4 html

<html><head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home Page - WebApplication1</title>
    <link rel="stylesheet" href="/lib/bootstrap/dist/css/bootstrap.css">
    <link rel="stylesheet" href="/css/site.css">
</head>
<body class="modal-open">

    <div class="container body-content">


<a id="deleteModal" data-toggle="modal" data-target="#modal-action-id" class="btn btn-danger" href="/Home/Modal">
    <i class="glyphicon glyphicon-trash"></i>  Delete
</a>
    </div>

    <div aria-labelledby="modal-action-label" role="dialog" tabindex="-1" id="modal-action-id" class="modal fade show" style="display: block;">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
        </div>
    </div>
</div>

    <script src="/lib/jquery/dist/jquery.js"></script>
    <script src="/lib/popper.js/dist/popper.js"></script>
    <script src="/lib/bootstrap/dist/js/bootstrap.js"></script>
    <script src="/js/site.js?v=ji3-IxbEzYWjzzLCGkF1KDjrT2jLbbrSYXw-AhMPNIA"></script>

<div class="modal-backdrop fade show"></div></body></html>

bootstrap 3.3.7 视觉结果

bootstrap 4.0.0 视觉结果

答案和问题

按照用户 mvermef 的建议添加此 js 脚本后,它可以工作...问题是?为什么使用 bs 3.3.7 也可以不使用???

 $(function () {
// boostrap 4 load modal example from docs
$('#modal-container').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget); // Button that triggered the modal
    var url = button.attr("href");
    var modal = $(this);
    // note that this will replace the content of modal-content everytime the modal is opened
    modal.find('.modal-content').load(url);
});

$('#modal-container').on('hidden.bs.modal', function () {
    // remove the bs.modal data attribute from it
    $(this).removeData('bs.modal');
    // and empty the modal-content element
    $('#modal-container .modal-content').empty();
    });
}); 

【问题讨论】:

标签: asp.net-mvc asp.net-core-mvc bootstrap-modal bootstrap-4


【解决方案1】:

Bootstrap 4 几乎是对 Bootstrap 3 的完全重写。因此,几乎没有 Bootstrap 3 代码在 Bootstrap 4 中工作。这只是完全重写的本质。如果要迁移到 Bootstrap 4,则必须逐个迁移。

这是一个在 Bootstrap 4 中工作的模式,甚至还有一个来自 FontAwesome 的图标(替换字形图标)。注意:container body-content 包装器在这里并没有真正使用:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="container body-content">

    <!-- Button trigger modal -->
    <button id="deleteModal" type="button" class="btn btn-danger" data-toggle="modal" data-target="#modal-action-id">
        <i class="fa fa-trash-o pr-2" aria-hidden="true"></i>Delete
    </button>

    <!-- Modal -->
    <div class="modal fade" id="modal-action-id" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    Do you really want to delete?
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
                    <button type="button" class="btn btn-danger">Yes</button>
                </div>
            </div>
        </div>
    </div>

</div>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

更多信息:

https://getbootstrap.com/docs/4.0/components/modal/

【讨论】:

  • 是的,我读到了谢谢,但模式看起来完全一样,我也做了一些所需的更改,如果你想玩的话,这里有我的 bootstrap 3.3.7 工作代码: )github.com/emiliano84/testModal/tree/master/WebApplication1ps模态有效,但仅适用于静态内容,我无法使用部分视图的内容填充它
  • 您是否还加载了所有 Bootstrap 4 文件,包括 jQuery 3.2.1、popper.js 和 bootstrap.js v.4(不加载任何旧的 Bootstrap 3 文件)?
  • P.S.出于测试和故障排除的目的,最好将完整的 HTML output 作为问题的一部分发布。这样问题就可以很快被隔离出来。
  • 我在问题中添加了我能做的一切
  • 检查我更新的代码 sn-p。这是一个完全工作的 Bootstrap 4 模式。您只需要调整“是”按钮的操作即可。
【解决方案2】:
$(function () {
// boostrap 4 load modal example from docs

$('#modal-container').on('show.bs.modal', function (event) {

    var button = $(event.relatedTarget); // Button that triggered the modal

    var url = button.attr("href");

    var modal = $(this);

    // note that this will replace the content of modal-content everytime the modal is opened
    modal.find('.modal-content').load(url);

});

$('#modal-container').on('hidden.bs.modal', function () {

    // remove the bs.modal data attribute from it

    $(this).removeData('bs.modal');

    // and empty the modal-content element

    $('#modal-container .modal-content').empty();

    });
}); 

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

    </div>
</div>

<a class="btn btn-primary" asp-action="Create" data-target="#modal-container" data-toggle="modal">New Flight</a>

这几乎是您在 asp.net(标准或核心)中模式所需的一切。适用于 BS 3.3.7 和 4.0

JavaScript 是调用 Action 方法的重要部分,它告诉模态框将 Partial 的内容推入其中。

【讨论】:

  • 感谢这项工作,我过去也阅读过有关此的文档,但由于 bootstratp 3.3.7 的某些原因,我不需要此脚本,您可以使用我的代码或 repo 自行尝试...这很尴尬
  • 我认为这是正确的答案,加载部分视图比投票的答案更有效。
  • @dave317 我同意,我在主要问题的末尾添加了一个答案和问题:为什么使用 bs 3.3.7 也没有? :)
  • 这在 MVC 5 项目中对我有用,但我无法使用部分视图在 .Net Core 中工作。有什么想法吗?
  • @dave317 抱歉回复晚了,为我工作,下载我的示例项目是 asp.net core 并添加解决方案中建议的 js github.com/emiliano84/testModal/tree/bootsrap4/WebApplication1
猜你喜欢
  • 2016-06-09
  • 2010-12-06
  • 1970-01-01
  • 2013-04-07
  • 1970-01-01
  • 2017-08-27
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
相关资源
最近更新 更多