ASP.NET MVC Bootstrap动态模态内容

Pow*_*ow5 5 asp.net asp.net-mvc jquery 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>
}
Run Code Online (Sandbox Code Playgroud)

Foreach (Var item in Model.Questions),会有一个button开放的modal.但是,我想modal根据item.idfrom 加载不同的内容Model.Questions.我怎样才能做到这一点?

Shy*_*yju 8

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

首先,在按钮中添加一个新的css类,稍后我们将用它来点击click事件以启动模式对话框.我们还将使用Url.Actionhtml帮助器方法生成详细信息视图的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>
}
Run Code Online (Sandbox Code Playgroud)

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

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

现在,让我们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();

        });
    });
});
Run Code Online (Sandbox Code Playgroud)

因此,当用户单击该按钮时,它会读取targeturldata属性的值(这是一个带有查询字符串中的项ID值的URL)并对该URL进行ajax调用.在我们的例子中,它将调用Detailsaction方法.所以让我们确保它返回局部视图结果(输出模态对话框内容)

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);
}
Run Code Online (Sandbox Code Playgroud)

部分视图将具有模态对话框所需的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>
Run Code Online (Sandbox Code Playgroud)