简体   繁体   English

如何创建自定义 Blazor 多选 HTML 组件? 错误:MultipleSelect 需要“ValueExpression”参数的值

[英]How to create a custom Blazor multiple select HTML component? Error: MultipleSelect requires a value for the 'ValueExpression' parameter

I am working to create a custom Blazor multiple select HTML component.我正在创建一个自定义 Blazor 多选 HTML 组件。 It works until I add the validation.在我添加验证之前它一直有效。 Also if I disable multiple select and leave the validation on, it works.此外,如果我禁用多项选择并保留验证,它可以工作。

When multiple select with validation is on I get this error:当启用带有验证的多项选择时,我收到此错误:

InvalidOperationException: MultipleSelect requires a value for the 'ValueExpression' parameter. Normally this is provided automatically when using 'bind-Value'.

I haven't been able to use the 'bind-Value' property because I get this error.我无法使用 'bind-Value' 属性,因为我收到此错误。

The documentation I have been able to find so far only address building a custom component from an HTML <select> element when the multiple select option is not in use.到目前为止,我能够找到的文档仅解决了在不使用多选选项时从 HTML <select>元素构建自定义组件的问题。

How do I go about creating a <select> element with multiple select enabled?如何创建启用多选的<select>元素?

Custom multiple select component自定义多选组件

MultipleSelect.razor MultipleSelect.razor

@using CustomComponents.DataModels
@using System.Linq.Expressions
@using System
@using System.Collections.Generic
@inherits InputBase<string>

<div class="row">
    <div class="col-3">
        <select id="@Id" @bind=@CurrentValue class="form-control @CssClass" multiple="multiple" size="@BoxHieght" style="width:@BoxWidth">
            @foreach (var option in Options)
            {
                <option @onclick="@(() => SelectOption(option))" value="@option.Value">@option.Text</option>
            }
        </select>
    </div>
</div>

@code {
    [Parameter]
    public string Id { get; set; }
    [Parameter]
    public List<Option> Options { get; set; } = new List<Option>();
    [Parameter]
    public Option SelectedOption { get; set; } = new Option { Text = " ", Value = " " };
    [Parameter]
    public int BoxHieght { get; set; } = 5;
    [Parameter]
    public string BoxWidth { get; set; } = "auto";
    [Parameter, EditorRequired]
    public Expression<Func<string>> ValidationFor { get; set; } = default!;

    private void SelectOption(Option option)
    {
        SelectedOption = option;
    }
    protected override bool TryParseValueFromString(string value, out string result, out string validationErrorMessage)
    {
        try
        {
            result = value;
            validationErrorMessage = null;
            return true;
        }
        catch (Exception exception)
        {
            result = null;
            validationErrorMessage = exception.Message;
            return false;
        }

    }    
}

Option data model object选项数据模型对象

Option.cs选项.cs

namespace CustomComponents.DataModels
{
    public class Option
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }
}

Web Form Model网络表单模型

FormModel.cs表单模型.cs

using CustomComponents.Data.DataModels;

namespace BlazorApp2.Pages.PageModels
{
    public class FormModel
    {
        public Option Option { get; set; } = new Option();
    }
}

Data Model数据模型

State.cs状态.cs

using System.ComponentModel.DataAnnotations;

namespace BlazorApp2.Data.DataModels
{
    public class State
    {
        [Required]
        public string Name { get; set; }
        [Required]
        public string Abbreviation { get; set; }
    }
}

Web Form网页表格

Index.razor索引.razor

@page "/"
@using CustomComponents.Components
@using CustomComponents.Data.DataModels
@using CustomComponents.Pages.PageModels
<PageTitle>Mutiple Select Component</PageTitle>

<EditForm Model="@model" OnValidSubmit="ValidSubmit">  
<DataAnnotationsValidator /> 
<ValidationSummary />
<MyComponent Options="@options" @bind-Value="@model.Option" ValidationFor="() => State.Name"></MyComponent>
Selected option:
<div class="row">
    <div class="col">
        @model.Option.Value  @model.Option.Text
    </div>

</div>
<div class="row">
    <div class="col">
        <button class="btn btn-primary" type="submit">Submit</button>
    </div>
</div>
<div class="row">
    <div class="col">
        @if (formSubmitted) @FormSubmitted
    </div>
</div>
<div class="row">
    <div class="col">
        @if (formSubmitted) @StateSubmitted
    </div>
</div>
</EditForm>

    @code {
    private List<Option> options = new List<Option>();
    public FormModel model = new FormModel();
    public State State { get; set; } = new State();
    private List<State> states = new List<State>
    {
        new State { Name = "Utah", Abbreviation = "UT" },
        new State { Name = "Texas", Abbreviation = "TX" },
        new State { Name = "Florida", Abbreviation = "FL" }
    };
    public string FormSubmitted { get; set; } = "Form submitted.";
    public string StateSubmitted { get; set; } = string.Empty;
    private bool formSubmitted = false;

    protected override void OnInitialized()
    {
        foreach(State state in states)
        {
            options.Add(new Option{ Value = state.Abbreviation, Text = state.Name});
        }
        model.Option = options[0];
    }

    public void ValidSubmit()
    {
        State.Abbreviation = model.Option.Value;
        State.Name = model.Option.Text;
        formSubmitted = true;
        StateSubmitted = $"{State.Abbreviation}  {State.Name}";
    }
}

because you inherit the component InputBase, you must use bind-value.因为继承了组件InputBase,所以必须使用bind-value。 I edited a lot of the code to make it work我编辑了很多代码以使其工作

@using BlazorApp2.Client.Components
@using System.Linq.Expressions
@using System
@using System.Collections.Generic
@using System.Diagnostics.CodeAnalysis
@inherits InputBase<Option>

<div class="row">
    <div class="col-3">
        <select id="@Id" class="form-control" size="@BoxHieght" style="width:@BoxWidth"
        @bind="OptionValueSelected"  @bind:event="oninput">
            @foreach (var option in Options)
            {
                <option value="@option.Value">@option.Text</option>
            }
        </select>

        <p>Selected option:@SelectedOption.Value </p>
    </div>
</div>

@code {
    [Parameter]
    public string Id { get; set; }
    [Parameter]
    public List<Option> Options { get; set; } = new List<Option>();
    [Parameter]
    public Option SelectedOption { get; set; } = new Option { Text = " ", Value = " " };
    [Parameter]
    public int BoxHieght { get; set; } = 5;
    [Parameter]
    public string BoxWidth { get; set; } = "auto";
    [Parameter, EditorRequired]
    public Expression<Func<string>> ValidationFor { get; set; } = default!;

    private string OptionValueSelected
    {
        get => CurrentValue.Value;
        set
        {
            CurrentValue = Options.Find(o => o.Value == value);
        }
    }

    protected override bool TryParseValueFromString(string value,
        [MaybeNullWhen(false)] out Option result, [NotNullWhen(false)] out string validationErrorMessage)
    {
        try
        {
            result = Options.First(o => o.Value == value.ToString());
            validationErrorMessage = null;
            return true;
        }
        catch (Exception exception)
        {
            result = null;
            validationErrorMessage = exception.Message;
            return false;
        }
    }
}

After very long research, Here are some important changes I made:经过很长时间的研究,以下是我所做的一些重要更改:

  • Inherits from InputBase of type Option not string.继承自 Option 类型的 InputBase 而不是字符串。 Reason: so the form context knows the type and binds correctly原因:所以表单上下文知道类型并正确绑定
  • Bind value with a property with setters.使用 setter 将值与属性绑定。 Reason: To convert from string to option原因:从字符串转换为选项
  • Set the value of SelectedOption from the Input base CurrectValue.从 Input base CurrectValue 设置 SelectedOption 的值。 Reason: To alert the form context about the change so that it updates the main view原因:提醒表单上下文有关更改,以便更新主视图

I Tested the component using this page on new project:我在新项目中使用此页面测试了组件:

@page "/"
@using BlazorApp2.Client.Components

<PageTitle>Index</PageTitle>

@code {
    List<Option> options = new List<Option>
    {
        new Option{Text = "Test1", Value = "Test1"},
        new Option{Text = "Test2", Value = "Test2"}
    };
    ExampleModel model;

    protected override void OnInitialized()
    {
        model = new ExampleModel();
    }
}

<h1>Hello, world!</h1>


<EditForm Model="@model">   
<MyComponent Options="@options" @bind-Value="@model.Option"></MyComponent>
</EditForm>
<p>@model.Option.Text : @model.Option.Value</p>

Example Model:示例模型:

public class ExampleModel
{
    public Option Option { get; set; } = new Option();
}

Resources helped me with my research :资源帮助我进行了研究:

Blazor components Blazor 组件

Blazor form components binding Blazor 表单组件绑定

Blazor Custom Binding Blazor 自定义绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM