Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind object to <select> option in Blazor?

I have the following dropdown menu:

public class object {
    public other_object apple {get; set;}
    ...
    public string stuff {get; set;}
    ...
}

public class other_object {
    public string id {get; set;}
    public string name {get; set;}
}

<select class="custom-select" @bind="@object.apple">
     <option value="@object.apple">@object.apple.name</option>
     ...
</select>

I want to bind the select to an object, but only want to display some attribute of that object. This will give me the error:

System.InvalidOperationException: The type 'other_object' does not have an associated TypeConverter that supports conversion from a string. Apply 'TypeConverterAttribute' to the type to register a converter.

Is this possible to do? I'm not really sure how the type converter thing works.

like image 840
mymemesarespiciest Avatar asked Dec 04 '19 13:12

mymemesarespiciest


People also ask

How do you bind a selected value in Blazor?

We can bind a drop-down list in Blazor WebAssembly using the <select> tag and bind the values to the drop-down list using the @bind attribute in the tag.

How do you bind a label to value in Blazor?

You can use the bind attribute on any element to bind the value. In blazor, we'll have a property assigned some value in the functions and use the property in the HTML template. Let's get this done. So, when we run the app, the label tag will display “red” as a text in the label.

What is binding in Blazor?

In general, @bind associates the current value of an expression with a value attribute and handles changes using the registered handler. Bind a property or field on other Document Object Model (DOM) events by including an @bind:event="{EVENT}" attribute with a DOM event for the {EVENT} placeholder.

How do you take input in Blazor?

The input element value is updated by the @bind event in Blazor. To get the current input value, use the oninput native event of the input element and get the current input value.

How do I bind data in Blazor?

In Blazor, you can bind data to both components and DOM elements using the bind attribute. Data binding is one of the most powerful features of software development technologies. It is the connection bridge between View and the business logic of the application. In Blazor, you can bind data in the following ways:

How to select multiple options in Blazor inputselect?

The 'colorsSelected' is the binding property of the 'select' element. Another way to get multiple option selection abilities is by using the 'InputSelect' blazor component. So key configuration to make our 'InputSelect' as multi-selection is by assigning its binding property as an array type.

What is selectedcolors in Blazor form field binding?

The 'SelectedColors' is an array type property that will be used for Blazor form field binding. Here we applied set validation rules like 'Required', 'MinLength', 'MaxLength'. Here 'MinLength' and 'MaxLength' check for the count of array type. Let's create a new Blazor component like 'Page2.razor'.

What is two-way binding in Blazor?

Two-Way Binding in Blazor Applications The two-way binding is a multi-directional binding technique. This means that the application and the user can update the values. Take the update form for example.


2 Answers

You cannot bind to other_object you can bind to a string property of other_object or use a TypeConverterAttribute to convert your other_object to a string.

Your code can be :

<select class="custom-select" @bind="@_selected.id">
     <option value="@object.apple.id">@object.apple.name</option>
     ...
</select>
@code {
    private other_object _selected = new other_object();
    ...
}
like image 166
agua from mars Avatar answered Oct 21 '22 10:10

agua from mars


You can do it like this in case for any reason you need the object to be created here:

<InputSelect @bind-Value="@object.apple.id" @onchange="@((args) => @object.apple = new other_object { id = (int)args.Value })" class="form-control">
      <option value="@object.apple.id">@object.apple.name</option>                        
</InputSelect>
like image 34
MarchalPT Avatar answered Oct 21 '22 12:10

MarchalPT