Home > other >  pass id in foreach to modal
pass id in foreach to modal

Time:01-03

i am trying to pass @item.PhanHoi.ID to modal but it @item.PhanHoi.ID only stops at first number of loop, is there any way to pass id to modal? Thanks

                            <tbody>
                                    @foreach (var item in ViewBag.DsPhanHoi)
                                    {
                                    <tr >
                                        <td></td>
                                        <td>@item.PhanHoi.Ten</td>
                                        <td>@item.PhanHoi.NoiDung</td>
                                        <td>@item.PhanHoi.TGphanhoi</td>
                                        <td>@item.PhanHoi.SDT</td>
                                        <td>@item.PhanHoi.Email</td>
                                        <td>@item.PhanHoi.DiaChi</td>
                                        <td >
                                            <div >
                                                <div  style="margin-left:26%">
                                                    <a  href="@Url.Action("XemPH", new { id = item.PhanHoi.ID })"><i style="font-size:15px"  aria-hidden="true"></i></a>
                                                    <a  data-toggle="modal" data-target="#DangerModalalert" data-item="@item.PhanHoi.ID"><i style="font-size:15px"  aria-hidden="true"></i></a>
                                                    <div id="DangerModalalert"  role="dialog">
                                                        <div >
                                                            <div >
                                                                <div >
                                                                    <a  data-dismiss="modal" href="#"><i ></i></a>
                                                                </div>
                                                                <div >
                                                                    <span ></span>
                                                                    <h2>Cẩn thận!</h2>
                                                                    <p>Việc này sẽ xóa phản hồi này vĩnh viễn!!!</p>
                                                                </div>
                                                                <div >
                                                                    <a data-dismiss="modal" href="#">Hủy bỏ</a>
                                                                    <a style="background-color:red" href="@Url.Action("XacNhanXoaPH", new {id =  @item.PhanHoi.ID })">Xóa!!</a>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>

                                        </td>
                                    </tr>
                                        
                                    }
                                </tbody>
                       

enter image description here

CodePudding user response:

The id of modal is not unique,you can try to make it unique,so that it will not always call the first modal.You can use the following code:

                    <tbody>
                            @{ var count = 0;}   
                            @foreach (var item in ViewBag.DsPhanHoi)
                            {
                            <tr >
                                <td></td>
                                <td>@item.PhanHoi.Ten</td>
                                <td>@item.PhanHoi.NoiDung</td>
                                <td>@item.PhanHoi.TGphanhoi</td>
                                <td>@item.PhanHoi.SDT</td>
                                <td>@item.PhanHoi.Email</td>
                                <td>@item.PhanHoi.DiaChi</td>
                                <td >
                                    <div >
                                        <div  style="margin-left:26%">
                                            <a  href="@Url.Action("XemPH", new { id = item.PhanHoi.ID })"><i style="font-size:15px"  aria-hidden="true"></i></a>
                                            <a  data-toggle="modal" data-target="#DangerModalalert_@count" data-item="@item.PhanHoi.ID"><i style="font-size:15px"  aria-hidden="true"></i></a>
                                            <div id="DangerModalalert_@count"  role="dialog">
                                                <div >
                                                    <div >
                                                        <div >
                                                            <a  data-dismiss="modal" href="#"><i ></i></a>
                                                        </div>
                                                        <div >
                                                            <span ></span>
                                                            <h2>Cẩn thận!</h2>
                                                            <p>Việc này sẽ xóa phản hồi này vĩnh viễn!!!</p>
                                                        </div>
                                                        <div >
                                                            <a data-dismiss="modal" href="#">Hủy bỏ</a>
                                                            <a style="background-color:red" href="@Url.Action("XacNhanXoaPH", new {id =  @item.PhanHoi.ID })">Xóa!!</a>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                </td>
                            </tr>
                            count  ;    
                            }
                        </tbody>
  • Related