aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Bug: element class with razor content and onclick gives compile error in generated class

Open JimLynn opened this issue 8 years ago • 3 comments

Add this code to the end of the Index.cshtml of a new Blazor client project.

<button
        @onclick(()=>dosomething())
        class="@name">Button</button>
<div>@Message</div>

@functions
{
    string name = "btn btn-primary";
    string Message = "";
    void dosomething()
    {
        Message = "Clicked";
    }
}

This will fail to build with the following error

Error CS1503	Argument 2: cannot convert from 'string' to 'Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeFrame'

The generated code in the intermediate file looks like this:

            builder.OpenElement(7, "button");
            builder.AddAttribute(8, "class", "");
            builder.AddAttribute(9, onclick(()=>dosomething()));
            builder.AddAttribute(10, name);
            builder.AddContent(11, "Button");
            builder.CloseElement();

You can see that it's put in a class attribute (8) with an empty string, then item 10 tries to put the name property in as an attribute, which is where the compile error is coming from.

However, if the order of the attributes is reversed, with the class coming before the onclick, it compiles fine.

JimLynn avatar Apr 06 '18 23:04 JimLynn

think this is a duplicate of aspnet/Blazor#386

yowl avatar Apr 06 '18 23:04 yowl

Looks like it. I did search but don't think that one came up.

JimLynn avatar Apr 06 '18 23:04 JimLynn

Thanks for reporting this. I do expect this issue no longer applies due to the new bind handling, but please let us know if you can still repro it after 0.2.0 is released this week.

SteveSandersonMS avatar Apr 09 '18 08:04 SteveSandersonMS