Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support multiple 'bind-Property' on components #16178

Closed
maraf opened this issue Apr 18, 2018 · 9 comments
Closed

Support multiple 'bind-Property' on components #16178

maraf opened this issue Apr 18, 2018 · 9 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@maraf
Copy link
Member

maraf commented Apr 18, 2018

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]
@danroth27
Copy link
Member

Which version of Blazor are you using?

@danroth27
Copy link
Member

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

@davidbmasterson
Copy link

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?

@maraf
Copy link
Member Author

maraf commented Apr 19, 2018

@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
Copy link
Member Author

maraf commented Apr 19, 2018

@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.

@rynowak
Copy link
Member

rynowak commented Apr 19, 2018

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

@SteveSandersonMS SteveSandersonMS changed the title Two-way binding of multiple component properties. Support multiple 'bind-Property' on components Apr 20, 2018
@rynowak
Copy link
Member

rynowak commented May 22, 2018

hi @maraf - I've added a test for this here: dotnet/blazor#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();

@maraf
Copy link
Member Author

maraf commented May 22, 2018

Cool, will try asap. Thanks!

@rynowak
Copy link
Member

rynowak commented May 22, 2018

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 rynowak closed this as completed May 22, 2018
@mkArtakMSFT mkArtakMSFT transferred this issue from dotnet/blazor Oct 27, 2019
@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Oct 27, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

5 participants