【问题标题】:Blazor navigation from within a loop?循环内的 Blazor 导航?
【发布时间】:2021-09-11 05:49:29
【问题描述】:

所以我正在尝试制作一个显示客户数据的 Blazor 应用程序。当显示所有客户时,我希望能够导航到单个客户。我无法完成这项工作。主要概念在下面的代码中,我也有预期的模型。

           @for(int i = 0; i < customers.Length-1; i++)
            {
            
                <tr>
                    <td>@customer.Id</td>
                    <td>@customer.CustomerId</td>
                    <td>@customer.LastName</td>
                    <td>@customer.FirstName</td>
                    <td>@customer.Name1056Form</td>
                    <td>@customer.TrapezeClientId</td>
                    <td>@customer.CustomerNote</td>
                    <td @onclick="NavToCustomer(i)"> Details</td>
                </tr>



      private void NavToCustomer(int i)
      {
       int Id = i;
        navigation.NavigateTo("/Customer/" + Id);

       }
          }

【问题讨论】:

标签: c# loops navigation blazor


【解决方案1】:
<td @onclick="()=>NavToCustomer(@customer.Id)"> Details</td>


private void NavToCustomer(int custID)
{
    navigation.NavigateTo("/Customer/" + custID);
}

解释:原版@onclick sends 有自己的参数(您可以通过将鼠标悬停在标记中的“@onclick”上来查看它们是什么)。您的事件处理程序不处理 MouseEventArgs,因此签名不匹配。您想发送自定义信息,因此您应该使用 lambda 表达式。

--编辑--

再想一想,你可以直接这样做吗?

<td @onclick="()=>navigation.NavigateTo("/Customer/" + customer.Id)"> Details</td>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多