Support multiple 'bind-Property' on components
Hi there, I have a user component with multiple properties which I want to bind to.
<User bind-Name="@UserName" bind-IsActive="@UserIsActive" />
- In
User, if I define *Changed delegate properties for bothNameandIsActive:
<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]
- 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]
Which version of Blazor are you using?
Can you please also share the code with the values you are trying to bind (UserName and IsUserActive)?
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?
@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.
@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 - thanks for sharing this, I'll take a look.
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();
Cool, will try asap. Thanks!
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!