aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Support multiple 'bind-Property' on components

Open maraf opened this issue 8 years ago • 9 comments

Hi there, I have a user component with multiple properties which I want to bind to.

<User bind-Name="@UserName" bind-IsActive="@UserIsActive" />
  1. In User, if I define *Changed delegate properties for both Name and IsActive:
<input type="text" bind="@Name" />
<input type="checkbox" bind="@IsActive" />

@functions
{
    public string Name { get; set; }
    public Action<string> NameChanged { get; set; }
    public bool IsActive { get; set; }
    public Action<bool> IsActiveChanged { get; set; }
}

Build fails with:

D:\Development\.NuGet\microsoft.aspnetcore.blazor.build\0.2.0\targets\RazorCompilation.targets(281,5): error : rzc generate exited with code 1. [D:\Development\Maraf\Money\src\Money.UI.Blazor\Money.UI.Blazor.csproj]
  1. When I originally defined only a single Changed property, generated code looked like:
builder.OpenComponent<Money.UI.Blazor.Components.User>(14);
builder.AddAttribute(15, "Name", Microsoft.AspNetCore.Blazor.Components.BindMethods.GetValue(UserName));
builder.AddAttribute(16, "NameChanged", new System.Action<System.String>(__value => UserName = __value));
builder.AddAttribute(17, "Name", Microsoft.AspNetCore.Blazor.Components.BindMethods.GetValue(UserIsActive));
builder.AddAttribute(18, "NameChanged", new System.Action<System.String>(__value => UserIsActive = __value));
builder.CloseComponent();

Which tried to bind both expressions to property Name and failed build with:

obj\Debug\netstandard2.0\Pages\Summary.g.cs(61,112): error CS0029: Cannot implicitly convert type 'string' to 'bool' [D:\Development\Maraf\Money\src\Money.UI.Blazor\Money.UI.Blazor.csproj]

maraf avatar Apr 18 '18 20:04 maraf

Which version of Blazor are you using?

danroth27 avatar Apr 18 '18 20:04 danroth27

Can you please also share the code with the values you are trying to bind (UserName and IsUserActive)?

danroth27 avatar Apr 18 '18 20:04 danroth27

I think non standard parameters are passed as strings , what happens if you change the public bool IsActive { get; set; } to a string and then convert it to a bool when you bind to the checkbox?

davidbmasterson avatar Apr 18 '18 20:04 davidbmasterson

@danroth27 Version is 0.2.0. When I try to bind only a single property, it compiles and works in the browser. Even when multiple properties are defined in the User component and only one of then is bound, it works. Here is minimal repro setup https://gist.github.com/maraf/7e16df79e7718cd5a2612985880071af.

@davidbmasterson First problem remains the same even if the properties are both of type string. Second part fails only because both properties (of different type) are bound to the same Changed property.

maraf avatar Apr 19 '18 05:04 maraf

@danroth27 Whole project can be found at maraf/Money/tree/two-way-bind-multiple-properties. The last commit to this branch causes that project Money.UI.Blazor won't compile.

maraf avatar Apr 19 '18 07:04 maraf

@maraf - thanks for sharing this, I'll take a look.

rynowak avatar Apr 19 '18 20:04 rynowak

hi @maraf - I've added a test for this here: https://github.com/aspnet/Blazor/pull/874 so I think that this is fixed in current dev and possibly in 0.3

The generated code in latest bits looks like:

            builder.OpenComponent<Test.User>(0);
            builder.AddAttribute(1, "Name", Microsoft.AspNetCore.Blazor.Components.BindMethods.GetValue(UserName));
            builder.AddAttribute(2, "NameChanged", Microsoft.AspNetCore.Blazor.Components.BindMethods.SetValueHandler(__value => UserName = __value, UserName));
            builder.AddAttribute(3, "IsActive", Microsoft.AspNetCore.Blazor.Components.BindMethods.GetValue(UserIsActive));
            builder.AddAttribute(4, "IsActiveChanged", Microsoft.AspNetCore.Blazor.Components.BindMethods.SetValueHandler(__value => UserIsActive = __value, UserIsActive));
            builder.CloseComponent();

rynowak avatar May 22 '18 18:05 rynowak

Cool, will try asap. Thanks!

maraf avatar May 22 '18 19:05 maraf

I'm closing this issue since I think we've addressed it. If this is still a problem, please reactivate or create a new issue and we will help you out. Thanks!

rynowak avatar May 22 '18 20:05 rynowak