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

如何解决Blazor中EditForm与Authorized的context参数名冲突问题?

解决Blazor中EditForm与AuthorizeView的context参数冲突错误

错误原因

<AuthorizeView>的<Authorized>模板默认会暴露一个名为context的参数(代表当前授权用户),而<EditForm>的子内容默认也会提供同名的context参数(代表表单编辑上下文)。即使手动指定了EditContext="@editContext",编译器仍会检测到参数名冲突。

解决方案

只需给其中一个组件的context参数显式重命名即可,推荐修改<Authorized>标签:

修改步骤

将原代码中的<Authorized>标签改为:

<Authorized Context="authContext">

这里的authContext是自定义参数名,如果你不需要用到当前用户信息,这个变量名可以随意设置,后续无需引用也不会影响功能。

修改后的完整代码

@page "/"

@using System.ComponentModel.DataAnnotations;
@using System.Globalization;
@inject ILogger<FreightCompany> Logger

<AuthorizeView>
    <Authorized Context="authContext">

        <EditForm EditContext="@editContext" OnSubmit="@Submit">
            <DataAnnotationsValidator />
            <ValidationSummary />

            <div class="row">
                <label>Link Name: @_model!.LinkName</label>
            </div>
            <div class="row">
                <label>Link Address:</label>
                <InputText type="time" class="m-1 form-control form-control-sm" @bind-Value="_model.LinkAddress" />
            </div>
            <div class="row">
                <button class="form-control btn btn-primary m-1" @onclick='()=>submitId="1"' type="submit">Submit</button>
                <button class="form-control btn btn-success m-1" @onclick='()=>submitId="2"' type="submit">Submit 2</button>
            </div>

        </EditForm>

        <div class="container">
            <div class="row">
                <table>
                    @if (_list.Count() > 0)
                    {
                        @foreach (var item in _list)
                        {
                            <tr>
                                <td><a href="@_model.LinkAddress">@_model.LinkName</a></td>
                            </tr>
                        }
                    }
                </table>
            </div>
        </div>

    </Authorized>
    <NotAuthorized>
        <p>Please signed in.</p>
    </NotAuthorized>
</AuthorizeView>


@code {
    string submitId = "";

    private EditContext? editContext;

    public FreightCompany? _model { get; set; }
    List<FreightCompany> _list = new();

    protected override void OnInitialized()
    {
        _model ??= new();        
        editContext = new(_model);
    }

    private void Submit()
    {
        switch (submitId)
        {
            case "1":
                Logger.LogInformation("Submit 1 pressed");
                var isValidForm = editContext!.Validate();
                if (isValidForm)
                {
                    _list.Add(_model!);
                    _model = new();
                    editContext = new(_model);
                }
                break;
            case "2":
                Logger.LogInformation("Submit 2 pressed");

                foreach (var item in _list)
                {
                    // SqliteDataAccess.SaveFreightLink(item);
                }

                break;
        }
        Logger.LogInformation("Model.Id = {Id}", _model?.Id);
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 15:49:50