简体   繁体   中英

ASP.NET MVC Using Modal popup with button onclick inside a jQuery datatable

Currently i'm doing a page which contains a table which contains some data. However, I want to put a bootstrap modal to show a message content inside the body of the popup. But I don't know how to process with the button because when i'm clicking on every buttons inside the table it shows me the same message content and it should not. Each rows, I mean most of them should not have the same message content.

@model Savoye.BridgeServices.Models.MessageModel

@{
    ViewData["Title"] = "Administration";
}

@{
    var data = ViewBag.Channels;
    var msg = ViewBag.Msg;
}

<link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.datatables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.js"></script>
<link href="https://cdn.datatables.net/1.10.15/css/dataTables.bootstrap.min.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/responsive/2.1.1/css/responsive.bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="container">
    <br />
    <form asp-controller="Administration" asp-action="AdminMessageManagement">
        <div class="row">
            <div class="col">
                <select name="getChannelValue">
                    <option value="">Select Channel</option>
                    @foreach (var item in data)
                    {
                        <option value="@item">@item</option>
                    }
                </select>
                <input type="text" name="getMessageKeyValue" placeholder="Message Key" />
                <select name="getIsDelivered">
                    <option value="">Is Delivered ?</option>
                    <option value=true>True</option>
                    <option value=false>False</option>
                </select>
                <input type="date" id="start" name="getDateFrom" value="@null" />
                <input type="submit" value="Filter" />
                <input type="button" value="Reset" onclick="location.href='@Url.Action("AdminTest", "Administration")'" />
            </div>
        </div>
    </form>
</div>
<br />

<table id="dataTable_id" class="table table-striped table-bordered dt-responsive nowrap" width="100%" cellspacing="0">
    <thead>
        <tr>
            <th></th>
            <th>Source Channel</th>
            <th>Message Key</th>
            <th>IsDelivered ?</th>
            <th>Target Channel</th>
            <th>Send Date</th>
            <th>Reception Date</th>
        </tr>
    </thead>

    <tbody>
        @foreach (var item in msg)
        {
            @foreach (var target in item.Targets)
            {
                <tr>  
                    <td class="details-control"><i class="fa fa-plus" data-toggle="modal" data-target=".bootstrapmodal" id="iconAdd" style="font-size:22px"></i></td>
                    <td>@item.SourceChannel</td>
                    <td>@item.MessageKey</td>
                    <td>@target.IsDelivered</td>
                    <td>@target.Channel</td>
                    <td>@item.Date</td>
                    <td>@target.DateDelivered</td>
                </tr>             
            }

        }
    </tbody>
</table>

@foreach (var item in msg)
{
    <div class="modal fade bootstrapmodal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">Message :</div>
                <div class="modal-body">@item.Content</div>
            </div>
        </div>
    </div>
}

    @section Scripts{
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.css">
        <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#dataTable_id').DataTable({
                    "processing": true,
                    "serverside": true,
                    "filter": false,
                    "paging": true
                });
            });
        </script>
    }

The popup is there :

@foreach (var item in msg)
{
    <div class="modal fade bootstrapmodal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">Message :</div>
                <div class="modal-body">@item.Content</div>
            </div>
        </div>
    </div>
}

and I would like that this button :

<td class="details-control"><i class="fa fa-plus" data-toggle="modal" data-target=".bootstrapmodal" id="iconAdd" style="font-size:22px"></i></td>

show me the Message content of each row in the table. I retrieved well, every data I need in the data except data in the popup. In the popup this is the same message content shown.

Where you make your rows replace the double foreach with this one:

@foreach (var item in msg)
{
    <div class="modal fade bootstrapmodal-@item">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">Message :</div>
                <div class="modal-body">@item.Content</div>
            </div>
        </div>
    </div>
    @foreach (var target in item.Targets)
    {
        <tr>
            <td class="details-control"><i class="fa fa-plus" data-toggle="modal" data-target=".bootstrapmodal-@item" id="iconAdd" style="font-size:22px"></i></td>
            <td>@item.SourceChannel</td>
            <td>@item.MessageKey</td>
            <td>@target.IsDelivered</td>
            <td>@target.Channel</td>
            <td>@item.Date</td>
            <td>@target.DateDelivered</td>
        </tr>
    }

}

and delete the foreach below the table where you make the modals, cause it is included in the double foreach now. This should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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