Binding to objects
Will the Bind syntax allow us to bind to objects instead of just primitive types. It throws an error "Error: System.ArgumentException: 'bind' does not accept values of type WebApplication1.Pages.Customer. To read and write this value type, wrap it in a property of type string with suitable getters and setters." Here is my code
<select class="form-control" bind="SelectedCustomer">
@foreach(var item in Customers)
{
<option value="@item">@item.Name</option>
}
</select>
The syntax is: @bind(SelectedCustomer) (for 0.1)
@dlr1 take a look at this comment https://github.com/aspnet/Blazor/issues/490#issuecomment-380953561
No, binding is only supported for a limited set of data types (for details see data binding chapter on learn-blazor.com).
I am using last friday's build
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.0-preview2-final" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Browser" Version="0.3.0-preview1-10206" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="0.3.0-preview1-10206" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<DotNetCliToolReference Include="Microsoft.AspNetCore.Blazor.Cli" Version="0.3.0-preview1-10206" />
</ItemGroup>
You can check the current status of supported types in https://github.com/aspnet/Blazor/blob/dev/src/Microsoft.AspNetCore.Blazor/Components/BindMethods.cs
This could make sense for “select”, but would not for any other type of form control.
We might do it for select at some point, but it will depend on what usage patterns arise and what level of community demand there is.
Thanks for the suggestion!
Closing as it’s not a planned work item for the time being.
How to bind select to object which is dictionary item. For example : new DictionaryItemModel(){ Id = 981, /* hidden id */ Description = "Some visible desc with utf chars" }
<select id="select-box" @bind="@SeletedDictionaryItem"> @foreach(var item in Lista){ }
The only samples I saw is using only description. It is impossible to bind and search using description bcause it is antiprogramming :)