Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor WebAssembly: Unhandled exception with Syncfusion SfMultiSelect on first selection due to NullReferenceException

$
0
0

I'm encountering a NullReferenceException when I try to make the first selection in a Syncfusion SfMultiSelect component embedded within a grid in my Blazor WebAssembly application. The error only occurs during the first selection attempt; subsequent selections work without issues. The specific error I'm getting is:

*Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]Unhandled exception rendering component: Object reference not set to an instance of an object.System.NullReferenceException: Object reference not set to an instance of an object.at Syncfusion.Blazor.Grids.Internal.Editors.ColumnsValidator`1[...].ValidateField(Object editContext, FieldChangedEventArgs fieldChangedEventArgs)at Microsoft.AspNetCore.Components.Forms.EditContext.NotifyFieldChanged(FieldIdentifier& fieldIdentifier)...*

Relevant Code:

Here is the relevant part of my UserGroup model:

public class UserGroup{    public int Id { get; init; }    //...more not relevant properties    [NotMapped]    [RequiredOnAdd(true, ErrorMessage = "you must select at least one employee")]    public List<int> EmployeeIds { get; set; } = new List<int>();}public class RequiredOnAdd : ValidationAttribute{    private readonly bool _isRequired;    public RequiredOnAdd(bool isRequired = true)    {        _isRequired = isRequired;    }    protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)    {        var entityType = validationContext.ObjectType;        var keyProperty = entityType.GetProperty("Id");        if (keyProperty == null)            return new ValidationResult("Id property not found on the entity.");        var keyValue = keyProperty.GetValue(validationContext.ObjectInstance);        var isAdding = keyValue == null || keyValue.Equals(GetDefaultValue(keyProperty.PropertyType));        if (_isRequired && isAdding)        {            if (value is not List<int> employeeIds || !employeeIds .Any())                return new ValidationResult(Resources.App.Validation_required, new[] { validationContext.MemberName ?? "" });        }        return ValidationResult.Success;    }    private static object? GetDefaultValue(Type type) => type.IsValueType ? Activator.CreateInstance(type) : null;}

Here is the Razor template where the SfMultiSelect is used:

<Template>    @{        var userGroup = (context as UserGroup);        userGroup.EmployeeID = 0; //for other purposes    }<div><SfMultiSelect @ref="MsEmployees" TValue="List<int>" AllowFiltering="true" TItem="Employee" Placeholder="@Employee" Mode="@VisualMode.Box" DataSource="@Employees" @bind-Value="@(userGroup!.EmployeeIds)"><MultiSelectFieldSettings Text="Name" Value="Id"></MultiSelectFieldSettings><MultiSelectEvents TValue="List<int>" TItem="Employee" Filtering="@ValueChangeHandlerRad" /><ValidationMessage For="@(() => userGroup.EmployeeIds)"></ValidationMessage></SfMultiSelect></div></Template>

Question:What could be causing this NullReferenceException during the first selection in SfMultiSelect, and how can I prevent it from happening? So only on first select, if I remove what I have selected and then select again, I do not get an error.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>