Aspnetcore: Templating not working on parameters - Blazor

Created on 21 Dec 2019  路  4Comments  路  Source: dotnet/aspnetcore

Describe the bug

I have this razor generic component TModel with parameters of Expression>[] parameter
now when the parent component of the previous one gives TModel type WorkOrder
the Expression> remains TModel and therefore i can`t pass WorkOrder expressions !

Code

DataGridComponent.razor.cs

public partial class DataGridComponent<TModel> : ComponentBase , IDisposable
    where TModel : DbModel
    {
        [Parameter]
        public Expression<Func<TModel, object>>[] Properties { get; set; }
}

SomeViewModel.razor

<div>
<DataGridComponent TModel="WorkOrder" Orderable="true" Indexable="true" 
Properties="new Expression<Func<WorkOrder, object>>[] {    <--------- Syntax Error
                                                                    a=>a.Location.LocationType,
                                                                    a=>a.MainService
                                                                }"></DataGridComponent>
</div>

--Cannot covert .... WorkOrder to ... TModel

Further technical details

  • ASP.NET Core version : 3.1
  • The IDE (VS 16.4 prev. 4)
affected-few area-blazor bug severity-blocking

Most helpful comment

Yay !
first time i issue a real issue :p

All 4 comments

further investigation..
it works if it is not an array of expression
one expression worked w/o errors...

even more...
it worked fine if it is List<> but not array...
this is a workaround i guess :)

Thanks for the issue report @mRmEEeZ. This looks like a bug in code generation when arrays are involved with generic components. Here's a much simpler reproduction of the problem:

```C#
public partial class TestComponent : ComponentBase where TModel : class
{
[Parameter]
public IList Prop1 { get; set; }

[Parameter]
public TModel[] Prop2 { get; set; }

}


```razor

@{ 
    var x = new[] { "Hello", "World" };
}
<TestComponent TModel="string" Prop1="x" Prop2="x"></DataGridComponent>

The generated code for the IList property does the correct type inference, but the array one does not perform substitution:

// IList
__builder.AddAttribute(5, "Prop1", Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<System.Collections.Generic.IList<WorkOrder>>(
// Array
__builder.AddAttribute(6, "Prop2", Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<TModel[]>(

@mRmEEeZ given that using IList works for you, I'll recommend using that.

Yay !
first time i issue a real issue :p

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aurokk picture aurokk  路  3Comments

ermithun picture ermithun  路  3Comments

githubgitgit picture githubgitgit  路  3Comments

FourLeafClover picture FourLeafClover  路  3Comments

Kevenvz picture Kevenvz  路  3Comments