繁体   English   中英

如何将数据传递到 ASP.NET MVC 中的模态视图

[英]How do I pass data to modal view in 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>

谢谢!

检查我的代码我希望你理解我的代码。

例如。 这是完成 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();
}

如果您对代码有疑问,这对我来说非常有用,请询问我,我会帮助您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM