C# Asp.net MVC将值传递给modal
我从我的视图中有一个表,它从我的Mysql数据库中加载数据,一旦单击表中的按钮update,它将弹出一个窗口,假设从我的表中加载详细信息,我的问题是它没有得到正确的值,每次我在我的行表上单击任何一个,它都将具有相同的值 以下是我的看法:C# Asp.net MVC将值传递给modal,c#,asp.net-mvc,html,C#,Asp.net Mvc,Html,我从我的视图中有一个表,它从我的Mysql数据库中加载数据,一旦单击表中的按钮update,它将弹出一个窗口,假设从我的表中加载详细信息,我的问题是它没有得到正确的值,每次我在我的行表上单击任何一个,它都将具有相同的值 以下是我的看法: <table border="1"> <thead> <tr> <th>OP</th> <th>OP Desc</th>
<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">×</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>
操作
操作说明
@foreach(模型中的var项目)
{
@item.op
@项目.操作说明
使现代化
更新选定值:
&时代;
作品:
@item.op
操作说明:
@项目.操作说明
关
保存更改
因此,我没有在当前选项卡上显示,而是决定将其传递给modal,使其成为弹出窗口问题是您的所有按钮都指向同一个modal:
data-target="#exampleModalCenter"
并且所有modals都具有相同的id(不是有效的HTML):
要解决此问题,您需要为每个模式提供一个唯一的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...
@foreach(模型中的变量项)
{
你们应该通过按钮传递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...