0

我正在尝试在 blazor Web 程序集应用程序中提交表单,并且在表单中,我使用了一个自定义组件,该组件将自动完成字段(用于公司名称)。目的是重用这个组件。

我正在使用内置的 EditForm 验证,并且我已经成功地根据需要声明了子组件字段(在下面的子组件 Company.razor 中),但我无法将字段信息传递给子组件,并且验证消息没有启动在公司名称字段中。

在此处输入图像描述

我正在使用 mudblazor,下面是我的组件的用法。我的问题是我如何传递类似的东西,For="@(() => _company.Name)"以便验证消息开始像LastName字段一样显示。

<EditForm EditContext="_editContext" OnSubmit="OnSubmit">
    <MudTextField Required Label="Last name" Class="mt-3" @bind-Value="_model.LastName" For="@(() => _model.LastName)"/>
<CompanyLogo CompanyName="@_company.Name" Required="true" @bind-Company="_company"/> 
</EditForm>

@code { 
    Company _company = new Company(){};
    private EditContext? _editContext;
    private ValidationMessageStore _messageStore;

    readonly CompleteProfile _model = new CompleteProfile();
    ....
    }

子组件:Company.razor

<MudAutocomplete Required="@Required"  RequiredError="Select a company name" T="Company" Label="Company name" 
                 @bind-Value="_company"
                 SearchFunc="@SearchForCompanyName" ToStringFunc="@(element => element == null ? null : $"{element.Name}")" MinCharacters="3" DebounceInterval="200">
    <ItemTemplate Context="element">
        <MudText>
            <img class="img" height="25" src=@ImageSource(element)> @($"{element.Name}")
        </MudText>
    </ItemTemplate>
</MudAutocomplete>


@code {

    Company _company;

    [Parameter]
    public bool Required { get; set; }

    [Parameter]
    public Company Company { get; set; }

    [Parameter]
    public string CompanyName { get; set; }

    [Parameter]
    public EventCallback<Company> CompanyChanged { get; set; }

    public string ImageSource(Company company)
    {
      return null; // code remove to keep it simple
      }
    }

----------


这是我的第一个 blazor 应用程序,非常感谢任何 helo。谢谢。

4

0 回答 0