【问题标题】:pass the data out of @Model to a form in the same razor page将数据从@Model 传递到同一剃须刀页面中的表单
【发布时间】:2021-12-31 12:37:55
【问题描述】:

我想将@item.Id 的值传递给此循环之外的表单,该表单位于同一剃须刀页面中

@foreach (var item in Model.GetContracts)
        {
    <tr>
        <td>@item.Plate</td>
        <td>@item.Cname</td>
    
        <td>@item.CheckIn.ToString("dd/MM/yyyy")</td>
        <td>@item.CheckOut.ToString("dd/MM/yyyy")</td>
        <td>@item.Price</td>
        <td>@item.Paid</td>
        <td>@item.Deposit</td>
        <td>@item.Note</td>
        <td>@item.Id</td>
    
        <td><Button type="button" class="btn btn-success" data-toggle="modal" data-target="#Returned" onclick="getId">إغلاق العقد</Button></td>
        <td><a class="btn btn-warning" asp-page="Returned" asp-route-Id="@item.Id">تجديد</a></td>
        
        
            
        
    </tr>
      }

我想将@item.Id 放入 asp-route-id="@item.Id"

<!-- Modal for returned -->
<div class="modal fade" id="Returned" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle"></h5>
                <button type="button" cl ass="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form method="post" asp-page-handler="Returned" asp-route-id="">
                    
                    

                    
                    <input type="date" asp-for="@Model.Update" />

                    <input type="submit" class="btn btn-primary" />
                </form>
            </div>
        </div>
    </div>
</div>

ps:我无法将表单放入循环中,因为它总是会读取列表中第一项的@item.Id,因为“div 不能嵌套在 tr 或 table 中”

【问题讨论】:

    标签: c# asp.net-core razor razor-pages asp.net-core-razor-pages


    【解决方案1】:

    您可以将表单添加为表格的单独列并在那里传递您的 ID。

    @foreach (var item in Model.GetContracts)
    {
    <tr>
        <td>@item.Plate</td>
        <td>@item.Cname</td>
    
        <td>@item.CheckIn.ToString("dd/MM/yyyy")</td>
        <td>@item.CheckOut.ToString("dd/MM/yyyy")</td>
        <td>@item.Price</td>
        <td>@item.Paid</td>
        <td>@item.Deposit</td>
        <td>@item.Note</td>
        <td>@item.Id</td>
    
        <td><Button type="button" class="btn btn-success" data-toggle="modal" data-target="#Returned" onclick="getId">إغلاق العقد</Button></td>
        <td><a class="btn btn-warning" asp-page="Returned" asp-route-Id="@item.Id">تجديد</a></td>
        <td>
            <div class="modal-body">
                <form method="post" asp-page-handler="Returned" asp-route-id="@item.id">
                   <input type="date" asp-for="@Model.Update" />
    
                    <input type="submit" class="btn btn-primary" />
                </form>
            </div>
        </td>
    </tr>
    }
    

    【讨论】:

      【解决方案2】:

      正如@Yuri 所说,您可以将模型弹出窗口放在 TD 和 foreach 循环中。但是你会发现它仍然会调用第一个模型,因为data-target="#Returned" 只是为#Returned 设置目标以避免此操作,我建议你可以尝试修改代码以使用for 循环而不是使用 foreach并使用return_@i 设置每一行的返回模型弹出ID。

      更多细节,您可以参考以下代码:

      @for (int i=0; i<Model.Count; i++)
      {
          
          <tr>
          <td>@Model[i].Plate</td>
          <td>@Model[i].Cname</td>
      
          <td>@Model[i].CheckIn.ToString("dd/MM/yyyy")</td>
          <td>@Model[i].CheckOut.ToString("dd/MM/yyyy")</td>
          <td>@Model[i].Price</td>
          <td>@Model[i].Paid</td>
          <td>@Model[i].Deposit</td>
          <td>@Model[i].Note</td>
          <td>@Model[i].Id</td>
             
              
          <td><Button type="button" class="btn btn-success" data-toggle="modal" data-target="#Returned_@i" onclick="getId">إغلاق العقد</Button></td>
      
          <td><a class="btn btn-warning" asp-page="Returned" asp-route-Id="@Model[i].Id">إغلاق العقد</a></td>
          <td><a class="btn btn-warning" asp-page="CheckOut" asp-route-Id="@Model[i].Id">تجديد</a></td>
      
          <td>
              <div class="modal fade" id="Returned" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                  <div class="modal-dialog modal-lg" role="document">
                      <div class="modal-content">
                          <div class="modal-header">
                              <h5 class="modal-title" id="exampleModalLongTitle"></h5>
                              <button type="button" cl ass="close" data-dismiss="modal" aria-label="Close">
                                  <span aria-hidden="true">&times;</span>
                              </button>
                          </div>
                          <div class="modal-body">
                              <form method="post" asp-page-handler="Returned" asp-route-id="@Model[i].Id">
                                  @item.Id
      
      
      
                                  <input type="date" asp-for="@Model[i].Update" />
      
                                  <input type="submit" class="btn btn-primary" />
                              </form>
                          </div>
                      </div>
                  </div>
              </div>
          </td>
          <td>
                  <div class="modal fade" id="Returned_@i" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                      <div class="modal-dialog modal-lg" role="document">
                          <div class="modal-content">
                              <div class="modal-header">
                                  <h5 class="modal-title" id="exampleModalLongTitle"></h5>
                                  <button type="button" cl ass="close" data-dismiss="modal" aria-label="Close">
                                      <span aria-hidden="true">&times;</span>
                                  </button>
                              </div>
                              <div class="modal-body">
                                  <form method="post" asp-route-id="@Model[i].Id">
                                      @Model[i].Id            
                                      <input type="date" asp-for="@Model[i].Update" />  
                                      <input type="submit" class="btn btn-primary" />
                                  </form>
                              </div>
                          </div>
                      </div>
                  </div>
              </td>
      
        
      </tr>
      
      }
      

      【讨论】:

      • 非常感谢它的工作
      猜你喜欢
      • 2020-05-13
      • 2022-10-13
      • 2018-05-30
      • 2021-04-05
      • 2020-01-30
      • 2021-12-29
      • 2019-05-17
      • 2021-06-29
      • 1970-01-01
      相关资源
      最近更新 更多