You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor中如何在EditForm内获取SfListView模板的正确上下文?

解决EditForm内SfListView模板上下文冲突问题

当在EditForm组件内部使用SfListView时,EditForm会将模板上下文默认设置为EditContext,覆盖了SfListView模板原本应该提供的AttachmentFile类型数据源项,这就是你遇到CS0039转换错误的原因。

解决方案:显式指定模板上下文名称

在SfListView的Template标签上通过Context参数,为ListView的数据源项指定一个自定义的变量名,避免和EditForm的默认context变量冲突。修改后的代码如下:

<EditForm EditContext="@myContext" OnSubmit="OnSendClick">
    <SfListView EnableVirtualization="true" DataSource="@attachments">
        <ListViewFieldSettings TValue="AttachmentFile" Id="@nameof(AttachmentFile.Id)" Text="@nameof(AttachmentFile.Name)">
        </ListViewFieldSettings>
        
        <ListViewTemplates TValue="AttachmentFile">
            <!-- 显式指定上下文变量名为fileItem -->
            <Template Context="fileItem">
                <div class="rezmessage-attachment-file">
                    <i class="@fileItem.Icon"></i>
                    <!-- 修正:添加@符号渲染变量值 -->
                    <a href="@fileItem.WebPath" target="_blank">@fileItem.Name</a>
                </div>
            </Template>
        </ListViewTemplates>
    </SfListView>
</EditForm>

说明

  • 通过Context="fileItem",SfListView会将当前遍历的AttachmentFile实例直接绑定到fileItem变量上,无需再进行类型转换。
  • 同时修正了原代码中a标签内的fileItem.Name未添加@符号的问题,确保变量值能正确渲染。

内容的提问来源于stack exchange,提问作者Yara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 16:42:39