Hello,
I try to list users on a grid.
The Razor page is:
<SfGrid DataSource="@DbContext.Users" Toolbar="@(new List<string>() {"Delete"})">
<GridEditSettings AllowAdding="false" AllowEditing="false" AllowDeleting="true"/>
<GridColumns>
<GridColumn Field=@nameof(User.Name) HeaderText="Name" TextAlign="TextAlign.Center" Width="160"/>
<GridColumn Field=@nameof(User.Email) ValidationRules="@VRs" HeaderText="Email"/>
</GridColumns>
</SfGrid>
The ViewModel:
public class UsersVM : PageVMBase
{
public ValidationRules VRs { get; set; } = new ValidationRules() {Email = true.ToString()};
}
The Model:
public class User
{
[Key]
public string Id { get; set; }
[Required(AllowEmptyStrings = false)]
public string FirstName { get; set; }
[Required(AllowEmptyStrings = false)]
public string LastName { get; set; }
public string Name => $"{FirstName} {LastName}";
[EmailAddress]
public string Email { get; set; }
public string Password { get; set; }
}
So nothing special.
The issue: when try to show the Email column the page breaks with the following error:
blazor.server.js:15 [2020-07-28T14:26:34.238Z] Error: System.InvalidCastException: Unable to cast object of type 'System.Boolean' to type 'System.String'.
at Syncfusion.Blazor.Grids.ValidationRules.ToInstance(IDictionary`2 dictionary)
at Syncfusion.Blazor.Grids.GridAnnotation.MapAnnotation(GridColumn columns, Type type)
at Syncfusion.Blazor.Grids.SfGrid`1.Syncfusion.Blazor.Grids.IGrid.AnnotateColumn(GridColumn column)
at Syncfusion.Blazor.Grids.GridColumn.OnInitializedAsync()
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
Without the EmailAddress attribute it is working well.
I tried to solve it by passing validation rules for the column (not sure it would be the solution), but:
- I couldn't set it on the razor page, just binding from the VM
- How to declare the ValidationRules.Email property? It's a string, but I couldn't find any documentation what is it.
Can you give me a hint what to do? It's a bug or normal behavior?
I am using the latest version: Sf.Blazor v18.2.0.47