ASP.NET MVC Bootstrap 动态模态框内容

7
我正在使用MVC 5,我的viewModel中每个记录都有一个 <button> 按钮,如下所示:
<table class="table table-hover">
<tr>
    <th>
        @Html.DisplayNameFor(model => model.Questions.Single().QuestionType)
    </th>
    <th>
        @Html.DisplayNameFor(model =>model.Questions.Single().Scheme)
    </th>
</tr>

@foreach (var item in Model.Questions)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.QuestionType)
        </td>
        <td>
            <button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#myModal" >
                View
            </button>
        </td>
    </tr>
}

Foreach (Var item in Model.Questions),将会有一个button打开一个modal。但是,我希望这个modal根据Model.Questions中的item.id加载不同的内容。我该怎么做呢?


2
jQuery Ajax 是一种可行的方式。 - Ehsan Sajjad
3个回答

15

您可以使用Bootstrap模态对话框和ajax将详细信息/部分视图结果加载到模态对话框中。

首先,向按钮添加一个新的CSS类,我们稍后将使用它来连接单击事件以启动模态对话框。同时,我们将使用Url.Action HTML帮助程序方法生成详细视图的URL,并将其保存在按钮的HTML5数据属性中(我们稍后将使用此选项进行ajax调用)。

@foreach (var item in Model.Questions)
{
  <tr>
    <td>
        <button type="button" class="btn btn-success btn-sm  modal-link"  
                 data-targeturl="@Url.Action("Details","Home",new { id = item.Id })">
            View
        </button>
     </td>
   </tr>
}

现在将以下代码添加到您当前的视图(甚至是布局)中。这将充当我们模态对话框的容器。

<!-- The below code is for the modal dialog -->
<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>
</div>

现在,让我们将click事件绑定到任何带有“modal-link”css类的元素上。 我们之前已经将此类添加到我们的按钮中。

$(function () {

    $('body').on('click', '.modal-link', function (e) {
        e.preventDefault();

        $("#modal-container").remove();

        $.get($(this).data("targeturl"), function (data) {

            $('<div id="modal-container" class="modal fade">'+
              '<div class="modal-content" id= "modalbody" >'+ 
                    data + '</div></div>').modal();

        });
    });
});

当用户点击按钮时,它会读取 targeturl 数据属性的值(该值是一个带有查询字符串中项ID值的URL),并向该URL发出ajax调用。在我们的例子中,它将调用 Details 操作方法。因此,让我们确保它返回局部视图结果(模态对话框内容)。

public ActionResult Details(int id)
{
    // Get the data using the Id and pass the needed data to view.

    var vm = new QuestionViewModel();

    var question = db.Questions.FirstOrDefault(a=>a.Id==id);
    // To do: Add null checks

    vm.Id = question.Id;
    vm.Title = question.Title;
    vm.Description = question.Description;

    return PartialView(vm);
}

而部分视图将具有模态对话框所需的HTML标记。

@model YourNameSpace.QuestionViewModel
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
    </div>
    <div class="modal-body">

        <h4>@Model.Title</h4>
        <p>@Model.Description</p>
        <p>Some other content for the modal </p>

    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
    </div>
</div>

嘿@shyju,感谢你的教导,它运行良好。但是模态框似乎是全屏宽度的,而且没有淡入淡出效果。有什么解决方法吗? - Pow4Pow5
我相信现在已经为 OP 提供帮助太晚了,但是对于其他人来说,您可以避免全屏宽度并通过剪切底部代码片段(<div class="modal-content">...</div>)并将其粘贴到外部 <div class="modal-dialog">[在此处粘贴]</div> 中来创建淡入效果。 - Dave Smash
这个非常好用。顺便提一下,为了避免全屏,像Dave提到的那样,ponury-kostek(下面的其他答案与此答案结合使用)就可以解决问题!Shyju干得好。 - Robert Smith

1

如果你想要一个动态的模态框,这段代码对你会有帮助。(我希望是这样)

<table class="table table-hover "  >
            <thead>
                <tr>
                    <th>Sıra</th>
                    <th>PC Adı</th>
                    <th>Kullanıcı</th>
                    <th>Marka/Model</th>
                    <th>Departman</th>
                    <th>İşletim Sistemi</th>
                    <th>Office</th>
                </tr>
            </thead>
            <tbody>
                @{int i1 = 1;
                    foreach (var item in Model.devices)
                    {
                        <tr style="font-size:13px" >
                            <td>@i1</td>
                            <td>@item.DeviceName</td>
                            <td>@item.DeviceOwner</td>
                            <td>@item.DeviceBrand</td>
                            <td>@item.DeviceDepartment</td>
                            <td data-toggle="modal" data-target="#@i1"> @Model.softwares[i1 - 1].OSName</td>
                            <td data-toggle="modal" data-target="#@i1">@Model.softwares[i1 - 1].OfficeName</td>
                        </tr>

                        <div id="@i1" class="modal fade"  role="dialog">
                            <div class="modal-dialog">
                                <!-- Modal content-->
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h4 class="modal-title">İşletim Sistemi ve Office Detayları</h4>
                                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                                    </div>
                                    <div class="modal-body">
                                            <p>OS Adı:@Model.softwares[i1 - 1].OSName</p>
                                            <p>OS Lisans:@Model.softwares[i1 - 1].OSLicenceCode</p>
                                            <p>OS Fatura:@Model.softwares[i1 - 1].OSBillCode</p>
                                            <p>Office Adı:@Model.softwares[i1 - 1].OfficeName</p>
                                            <p>Office Lisans:@Model.softwares[i1 - 1].OfficeLicenceCode</p>
                                            <p>Office Fatura:@Model.softwares[i1 - 1].OfficeBillCode</p>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                    </div>
                                </div>

                            </div>
                        </div>

                        i1++;
                    }
                }

            </tbody>
        </table>

0

谢谢Shjiu。代码运行良好。要更改模型全屏,请添加此代码。

$(function () {
    $('body').on('click', '.modal-link', function (e) {
        e.preventDefault();
        $("#modal-container").remove();
        $.get($(this).data("targeturl"), function (data) {
            $('<div id="modal-container" class="modal fade">  <div class="modal-dialog modal-lg" style="width:500px;>' +
                '<div class="modal-content" id= "modalbody" >' + 
                data + 
                '</div></div></div>').modal();
        });
    });
});

这个修改和Shyju的原始代码一起非常好用!现在弹出窗口已经居中了。 - Robert Smith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接