简体   繁体   English

在asp.net mvc中使用foreach循环显示模态

[英]Display modal with a foreach loop in asp.net mvc

I have a table that is created using a foreach.我有一个使用 foreach 创建的表。 This is half of the table with the foreach to display the values from the database.这是带有 foreach 的表的一半,用于显示数据库中的值。

     @foreach (var item in Model)
                {
                    <tr>

                        <td>
                            @Html.DisplayFor(modelItem => item.TaskName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.TaskAssignment)
                        </td>
                         <td>
                                @Html.DisplayFor(modelItem => item.CCInstruction)
                            </td>

                        <td>
                            <select>
                                <option>Employer</option>
                                <option>Employee</option>
                                <option>Employee and Employer</option>
                            </select>
                        </td>
                        <td>
                            <input type="button" class="btn btn-default" value="Fields" onclick="window.location.href = '../../OBClientSetupTaskFields/Index/@item.SetupID?tid=@item.TaskID'" />
                            <input type="button" class="btn btn-default" value="Documents" />
                            <input type="button" class="btn btn-default" data-toggle="modal" data-target="#instructions" value="Instructions" />
                            <input type="button" class="btn btn-default" value="tips" />
 <!--Modals-->
                        <div class="modal fade" id="instructions" tabindex="-1" role="dialog" aria-labelledby="instructionsLabel">
                            <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title" id="instructionsLabel">Instructions</h4>
                                    </div>
                                    <div class="modal-body">
                                        <p>Placeholder text for isntructions or anything of that sort.</p>
                                        @Html.TextAreaFor(modelItem => item.CCInstruction, new {@class = "form-control", @rows = "6", @style = "width: 80%;"})
                                        <p>Placeholder text for isntructions or anything of that sort.</p>
                                        @Html.TextAreaFor(modelItem => item.EEInstruction, new {@class = "form-control", @rows = "6", @style = "width: 80%;"})
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                        <button type="button" class="btn btn-primary">Save changes</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                        </td>

                    </tr>
                }

            </table>

That foreach displays each input available in the model.该 foreach 显示模型中可用的每个输入。 Since the code is in the foreach loop shouldn't these display each one when someone clicks the instructions button?由于代码在 foreach 循环中,当有人单击说明按钮时,这些代码不应该显示每个循环吗?

So what I noticed was that I was basically calling the same modal each time because the modal id wasn't dynamically changing with the foreach.所以我注意到我基本上每次都调用相同的模态,因为模态 id 没有随着 foreach 动态变化。 It was just the same modal over and over.它一遍又一遍地使用相同的模式。 So I concatenated the taskID on the end of the modal name and in the url so it would call something like this:所以我在模式名称的末尾和 url 中连接了 taskID,所以它会调用这样的东西:

<input type="button" class="btn btn-default" data-toggle="modal" data-target="#tips-@item.TaskID" value="Tips" />

div class="modal fade" id="tips-@item.TaskID" tabindex="-1" role="dialog" aria-labelledby="tipsLabel">

Each taskID is unique so it won't be the same ever in the foreach loop.每个 taskID 都是唯一的,因此它在 foreach 循环中永远不会相同。

So when the HTML generates it output this:所以当 HTML 生成它时输出:

<input type="button" class="btn btn-default" data-toggle="modal" data-target="#instructions-10" value="Instructions">

<div class="modal fade" id="instructions-10" tabindex="-1" role="dialog" aria-labelledby="instructionsLabel">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                                    <h4 class="modal-title" id="instructionsLabel">Instructions</h4>
                                </div>
                                <div class="modal-body">
                                    <p>Placeholder text for isntructions or anything of that sort.</p>
                                    <textarea class="form-control" cols="20" id="item_CCInstruction" name="item.CCInstruction" rows="6" style="width: 80%;">Testing 2</textarea>
                                    <p>Placeholder text for isntructions or anything of that sort.</p>
                                    <textarea class="form-control" cols="20" id="item_EEInstruction" name="item.EEInstruction" rows="6" style="width: 80%;">Testing 2</textarea>
                                </div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                                    <button type="button" class="btn btn-primary">Save changes</button>
                                </div>
                            </div>
                        </div>
                    </div>

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

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