Asp.net MVC将值传递给模态

时间:2018-10-27 01:07:23

标签: c# asp.net-mvc html5

我从我的视图中得到一个表,它从Mysql数据库中加载数据,一旦您单击表中的按钮更新,它将弹出一个窗口,据说该窗口会从我的表中加载详细信息,而我的问题是获取正确的值,每次我在行表上单击任何一个时,它都将具有相同的值。

这是我的观点:

<table border="1">
<thead>
    <tr>
        <th>OP</th>
        <th>OP Desc</th>                
    </tr>
</thead>
<tbody>
  @foreach (var item in Model)
    {
        <tr>
            <td>@item.op</td>
            <td>@item.op_desc</td>
            <td>                            

                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">Update</button>

                <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLongTitle"><b>Update selected values:</b></h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <dl class="dl-horizontal">
                                    <dt>
                                        OP:
                                    </dt>
                                    <dd>
                                        @item.op
                                    </dd>
                                    <dt>
                                        OP Desc:
                                    </dt>
                                    <dd>
                                        @item.op_desc                                               
                                </dl>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                <button type="button" class="btn btn-primary">Save changes</button>
                            </div>
                        </div>
                    </div>
                </div>

            </td>
        </tr>
    </tbody>
</table>

所以我决定不将其显示在当前标签上,而是将其传递给模式以使其弹出窗口

1 个答案:

答案 0 :(得分:1)

问题在于您所有的按钮都指向相同的模式:

data-target="#exampleModalCenter"

,并且所有模态都具有相同的ID(这不是有效的HTML):

id="exampleModalCenter"

要解决此问题,您需要为每个模式赋予唯一的ID,如下所示:

@foreach (var item in Model)
{

   <button data-toggle="modal" data-target="#@Model.UseSomeUniqueProperty" // other attributes...
   <div id="@Model.UseSomeUniqueProperty" class="modal fade" // other attributes

   // rest of your code...