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.
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.
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.
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.
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.
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:
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.
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'.
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.
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();
...
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With