简体   繁体   English

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

[英]How do I pass data to modal view in ASP.NET MVC

I am having problems passing data to modal view.我在将数据传递到模态视图时遇到问题。 Either modal view doesn't open at all or reads data from first item every time I open it.模态视图根本没有打开,或者每次打开它时都从第一项读取数据。 The biggest problem I have is that I don't know jQuery.我遇到的最大问题是我不知道 jQuery。 I want a button to open a modal that reads a variable of my item in this case item.Code.我想要一个按钮来打开一个模式,在这种情况下,item.Code 读取我的项目的变量。 Items are in a foreach loop.项目在 foreach 循环中。

This is how i want my code to look.这就是我希望我的代码的外观。

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

I tried it with and without a partial view.我在有和没有局部视图的情况下尝试过。 This is the code I currently have which just opens a modal window but doesn't show anything else.这是我目前拥有的代码,它只打开一个模态窗口但不显示任何其他内容。 I know the problem is in id's, but I tried to change all id's to unique ones which caused even more problems.我知道问题出在 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>

Thank you!谢谢!

Check my code i hope you are understanding with my code.检查我的代码我希望你理解我的代码。

Eg.例如。 This is the your html after the complete the forLoop这是完成 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 Modal 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>

Scripts脚本

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

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

This is perfectly working for me if you have some problem with code the please ask me i will help you.如果您对代码有疑问,这对我来说非常有用,请询问我,我会帮助您。

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

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