【问题标题】:.netCore How to pass data to controller from table of list?.net Core 如何从列表中将数据传递给控制器​​?
【发布时间】:2021-10-18 19:42:31
【问题描述】:

我的视图中有一个对象列表。在表格中,在某些条件下,我创建行并添加一个按钮。当按下 modal-footer 按钮时,我想将该行的数据传递给我的控制器。我尝试了 ajax 请求和表单,但我无法管理它。到达我的控制器的总是列表的第一个元素。

我的看法:

<table class="table border-bottom dataTable" id="dataTable" width="100%" cellspacing="0" role="grid" aria-describedby="dataTable_info" style="width: 100%;">
            <thead>
                <tr><th rowspan="1">Saat</th><th rowspan="1">Durum</th></tr>
            </thead>
            @{int i = 0; }
            @foreach (var v in Model)
            {
                <tbody>
                    <tr>
                        @if (v.IsActive == 1)
                        {
                            <td>@v.Hour.ToString():@v.Minute.ToString().ElementAt(0)0</td>
                            <td><p class="blckpassive">Dolu</p></td>                           
                        }
                        else
                        {
                            <td>@v.Hour.ToString():@v.Minute.ToString().ElementAt(0)0</td>
                            <td><p class="blckactive">Boş</p></td>                                                                                  
                            <td><button type="button" class="btn btn-info" data-toggle="modal" data-target="#exampleModal">Randevu Al</button></td>
                            <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title" id="exampleModalLabel">Emin Misiniz?</h5>
                                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                            </button>
                                        </div>
                                        <div class="modal-body">
                                            Seçtiğiniz Randevuyu Almak İstiyor Musunuz?
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-dismiss="modal">İptal</button>
                                            <a type="button" href="/Appointment/AddAppointment?hour=@v.Hour" class="btn btn-primary">Evet</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        }
                    </tr>
                </tbody>
                i++; 
                
            }

        </table>

控制器:

public IActionResult AddAppointment(int hour)
        {
            return null;
        }

列表中的对象具有小时和分钟变量,它们是 int 并且排序为“9:00,9:30,10:00,10:30...16:00”。

【问题讨论】:

  • v.Hour 是该列表中的第一项吗?还是您的意思是Model 中的每个v 都有一个不同的.Hour,分别为 9、9:30、10...
  • 表格是否显示了正确的时间,它只是被打破,舔链接?
  • @MenachemHornbacher 显示正确数据的表,模型中的每个 v 都有不同的小时,是的。
  • 有趣,列表中的链接是什么。它们都具有相同的值还是仅在提交时才会发生?
  • @MenachemHornbacher 它们都有不同的值,来自 db。在提交时,我需要知道点击了哪个值,所以我需要来自该对象的数据。我也会用它填充数据库。

标签: c# html asp.net-core asp.net-core-mvc


【解决方案1】:

到达我的控制器的总是列表的第一个元素。

你把弹出模式对话框的代码放在一个foreach循环中,如果你查看html源代码,你可以发现data-target属性的多个按钮具有相同的值#exampleModal,并且目标模式(s ) 与id 属性的exampleModal 值相同,这会导致上述问题。

为了解决这个问题并使模态弹窗正常工作,您可以修改如下代码,使HTML文档中的按钮触发器的data-target属性和模态uniqueid属性.

<td><button type="button" class="btn btn-info" data-toggle="modal" data-target=@("#exampleModal"+i)>Randevu Al</button></td>
<div class="modal fade" id=@("exampleModal"+i) tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">

测试结果

【讨论】:

  • 我明白了,总是需要唯一的 ID。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 2021-04-27
相关资源
最近更新 更多