Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select box binding in blazor

I am trying to bind CountryId in the model to the value of a selected item of SelectList in Blazor. All of the Country items come in a list like {CountryId, CountryName} object. I do the code like so:

    <InputSelect @bind-Value="model.ByCountryId" class="form-control">         @if (model?.Countries != null)         {            @foreach (var cnt in model.Countries)            {                <option value="@cnt.Id">@cnt.Name</option>            }         }      </InputSelect> 

And code block:

@code {  BrandModel model = new BrandModel();  protected override async Task OnInitializedAsync() {     model = new BrandModel     {         Id = 19,         ByCountryId = 1,         Countries = new List<ent.Country>             {                 new ent.Country { Id = 1, Name = "Azerbaijan" },                 new ent.Country { Id = 2, Name = "Turkey" }             },         IsActive = true,         Name = "Brand"     }; } 

But this execution gives me this error in the browser:

blazor.webassembly.js:1 WASM: System.MissingMethodException: Constructor on type 'System.ComponentModel.ByteConverter' not found.

What is the convenient way of binding <select> and model.data in Blazor?

like image 788
Emba Ayyub Avatar asked Oct 21 '19 11:10

Emba Ayyub


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 Blazor?

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 following example binds the InputValue property to the <input> element's value when the element's oninput event ( input ) is triggered.

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 the difference between bind and bind value in Blazor?

In Blazor, there is no significant difference between using these two attributes. The @bind attribute is a shorthand of the @bind-value attribute and its delegate will trigger the ValueChanged event of the component.


1 Answers

It works well when I put the <InputSelect> in a <EditForm Model="@model">..</EditForm >and there's no problem in your data binding.

Try to use below code to set <BlazorLinkOnBuild>false</BlazorLinkOnBuild> in the csproj file.

<PropertyGroup>    <BlazorLinkOnBuild>false</BlazorLinkOnBuild> </PropertyGroup> 

Refer to https://github.com/aspnet/AspNetCore/issues/7784

Update:

Use <select> tag instead of <InputSelect> like

<select @bind="model.ByCountryId">         @if (model?.Countries != null)         {             @foreach (var cnt in model.Countries)             {                 <option value="@cnt.Id">@cnt.Name</option>             }         } </select> 
like image 79
Ryan Avatar answered Sep 28 '22 00:09

Ryan