aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Binding to objects

Open dlr1 opened this issue 8 years ago • 7 comments

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>

dlr1 avatar Apr 16 '18 13:04 dlr1

The syntax is: @bind(SelectedCustomer) (for 0.1)

floreseken avatar Apr 16 '18 14:04 floreseken

No, binding is only supported for a limited set of data types (for details see data binding chapter on learn-blazor.com).

rstropek avatar Apr 16 '18 14:04 rstropek

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>

dlr1 avatar Apr 16 '18 14:04 dlr1

You can check the current status of supported types in https://github.com/aspnet/Blazor/blob/dev/src/Microsoft.AspNetCore.Blazor/Components/BindMethods.cs

rstropek avatar Apr 16 '18 14:04 rstropek

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.

SteveSandersonMS avatar Apr 16 '18 14:04 SteveSandersonMS

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 :)

brzezinol avatar Oct 08 '19 10:10 brzezinol