ASP.NET MVC Bootstrap动态模态内容

时间:2017-03-23 18:25:04

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

我正在使用MVC 5,<button>中的每条记录都有viewModel,如下所示:

<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根据item.id中的Model.Questions加载不同的内容。我怎么能这样做?

3 个答案:

答案 0 :(得分:7)

您可以使用带有ajax的bootstrap模态对话框将详细信息/视图局部视图结果加载到模态对话框。

首先,在按钮中添加一个新的css类,稍后我们将用它来点击click事件以启动模式对话框。我们还将使用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>

现在,让我们通过&#34; modal-link&#34;连接任何元素上的click事件。 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)的值,并进行ajax调用那个URL。在我们的例子中,它将调用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>

答案 1 :(得分:0)

如果你想拥有动态模态,这段代码对你有用。(我希望如此)

<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>

答案 2 :(得分:-1)

谢谢Shjiu.Code工作正常。要更改全屏模型,请添加此代码。 '。

$(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();
        });
    });
});