首页 > 解决方案 > 如何将数据传递给 ASP.NET MVC 中的模式视图

问题描述

我在将数据传递到模态视图时遇到问题。模态视图根本不打开,或者每次打开它时都会从第一个项目中读取数据。我最大的问题是我不知道 jQuery。我想要一个按钮来打开一个模式,在这种情况下读取我的项目的变量 item.Code。项目在 foreach 循环中。

这就是我希望我的代码看起来的样子。

<div class="row" style="padding: 0px 50px">
@foreach (var item in Model)
{
    <div style="width: 200px; height:200px; border: 1px dotted #fff; text-align:center; display: inline-block">
        <h4>@item.Name</h4>

        <!-- Button to open modal view here for this specific item -->

    </div>
}

我在有和没有局部视图的情况下都试过了。这是我目前拥有的代码,它只打开一个模式窗口,但不显示其他任何内容。我知道问题出在 id 中,但我试图将所有 id 更改为唯一的,这会导致更多问题。

    @foreach (var item in Model)
{
    <div style="width: 200px; height:200px; border: 1px dotted #fff; text-align:center; display: inline-block">
        <h4>@item.Name</h4>
    </div>
    <input class="btn btn-default" type="button" value="Details" id="@item.id" onclick="Details(this.id);" />


    <div class="modal fade" id="myModal" role="dialog">
        <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">
                    @Html.Raw(item.Code);
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
}
<script>
    function Details(id) {
        $.get("@Url.Action("preview","Generator")/"+id, function (data) { $('.modal-body').html(data); });
        $('#myModal').modal('show');
    }

    $('#myModal').on('hidden.bs.modal' , function (e) {
        $('.modal-body').html("");
    })
</script>

谢谢!

标签: c#jqueryasp.nettwitter-bootstrapmodal-dialog

解决方案


例如。这是你完成forLoop后的html

<div class="row" style="padding: 0px 50px">
     <div style="width: 200px; height:200px; border: 1px dotted #fff; text-align:center; display: inline-block">
          <h4>Item Name - 1</h4>
          <button type="button" data-item="Item Name - 1" class="btn btn-default clsDetails">Details</button>
     </div>
     <div style="width: 200px; height:200px; border: 1px dotted #fff; text-align:center; display: inline-block">
          <h4>Item Name - 2</h4>
          <button type="button" data-item="Item Name - 2" class="btn btn-default clsDetails">Details</button>
     </div>
     <div style="width: 200px; height:200px; border: 1px dotted #fff; text-align:center; display: inline-block">
          <h4>Item Name - 3</h4>
          <button type="button" data-item="Item Name - 3" class="btn btn-default clsDetails">Details</button>
     </div>
</div>

HTML 模态

<div class="modal fade" id="myModal" role="dialog">
        <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">
                    <div class="dvData"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

脚本

$(document).ready(function(){
    $(document).on("click",".clsDetails",OpenModalPopUp);   
});

function OpenModalPopUp(){
   var itemName = $(this).data("item");
   alert(itemName);
   $('#dvData').html(itemName);
   $("#myModal").modal();
}

推荐阅读