我试图在一个MudTable中自定义一个MudLink的链接。
如此:
<MudTable Items="@TicketList" >
<RowTemplate>
<MudTd DataLabel="Subject">@context.Subject</MudTd>
</RowTemplate>
</MudTable>
但这不是:(编译错误:名称'context'在当前上下文中不存在。)
<MudTable Items="@TicketList" >
<RowTemplate>
<MudTd DataLabel="Subject"><MudLink Href="/ticket/Edit/@context.Id">@context.Subject</MudLink></MudTd>
</RowTemplate>
</MudTable>
如何根据行绑定数据在MudLink中生成Href ?
我不明白你发布的代码是如何导致你提到的错误的。当你尝试<MudLink Href="/ticket/Edit/@context.Id">
时,你会得到这样的错误:"组件属性不支持复杂的内容(混合c#和标记)……"。相反,您应该这样做:<MudLink Href="@($"/ticket/Edit/{context.Id}")">
.
下面是一个可以工作的演示:
<MudTable Items="@this.tickets">
<HeaderContent>
<MudTh>Id</MudTh>
<MudTh>Subject</MudTh>
<MudTh>Link</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Id">
@context.Id
</MudTd>
<MudTd DataLabel="Subject">
@context.Subject
</MudTd>
<MudTd DataLabel="Link">
<MudLink Href="@($"/ticket/Edit/{context.Id}")">@context.Id</MudLink>
</MudTd>
</RowTemplate>
</MudTable>
@code {
private List<Ticket> tickets = new();
protected override void OnInitialized()
{
this.tickets = new List<Ticket>
{
new() { Id = "123", Subject = "mu" },
new() { Id = "456", Subject = "foo" }
};
}
public class Ticket
{
public string Id { get; set; }
public string Subject { get; set; }
}
}
尝试在线:https://try.mudblazor.com/snippet/cumGuVltGdrWbDqC