Completed
Last Updated: 03 Jul 2019 11:47 by ADMIN
Release 1.3.0
Oliver
Created on: 04 Jun 2019 09:07
Category: DropDownList
Type: Bug Report
2
Binding to a null string Value throws NullReferenceException

When the Value you bind to the DropDownList is null (for example, because it is a model that is not filled in by the user yet, and you need to perform validation), the component throws a null reference exception.

This does not happen for a nullable integer (example here)

Reproducible:

@using System.ComponentModel.DataAnnotations
@using Telerik.Blazor.Components.DropDownList
 
    <EditForm Model="@PageData" OnValidSubmit="@HandleValidSubmit">
        <DataAnnotationsValidator />
        <ValidationSummary />
        @PageData.QuoteState
 
        <TelerikDropDownList bind-Value="@PageData.QuoteState" DefaultItem="@Hint" Width="300px"
                             Data="@states" TextField="stateName" ValueField="stateID">
        </TelerikDropDownList>
        <ValidationMessage For="@(() => PageData.QuoteState)"></ValidationMessage>
        <button type="submit">Submit</button>
    </EditForm>
 
@functions {
    public MyViewModel PageData { get; set; } = new MyViewModel();
 
    public statesModel Hint { get; set; } = new statesModel { stateID = null, stateName = "Not Selected" };
 
    public class statesModel
    {
        public string stateID { get; set; }
        public string stateName { get; set; }
    }
 
    public class MyViewModel
    {
        [Required(ErrorMessage = "State is mandatory.")]//the value field in the dropdown model must be null in the default item
        public string QuoteState { get; set; }
    }
 
    public IEnumerable<statesModel> states = new List<statesModel>
    {
            new statesModel { stateID = "ACT", stateName = "ACT" },
            new statesModel { stateID = "NSW", stateName = "NSW" },
            new statesModel { stateID = "NT", stateName = "NT" },
            new statesModel { stateID = "QLD", stateName = "QLD" },
            new statesModel { stateID = "SA", stateName = "SA" },
            new statesModel { stateID = "TAS", stateName = "TAS" },
            new statesModel { stateID = "VIC", stateName = "VIC" },
            new statesModel { stateID = "WA", stateName = "WA" }
        };
 
    void HandleValidSubmit()
    {
        Console.WriteLine("OnValidSubmit");
    }
}

2 comments
ADMIN
Marin Bratanov
Posted on: 11 Jun 2019 11:55
Hello Marcel,

Indeed, this is another case. We will have both in mind when working on this.

It requires a lot of changes and will probably be done together with binding to simple lists, so you may want to follow/vote on that as well, as it is also a major rework: https://feedback.telerik.com/blazor/1411181-telerikdropdownlist-should-accept-lists-of-scalar-as-data-source.


Regards,
Marin Bratanov
Progress Telerik UI for Blazor
Marcel
Posted on: 10 Jun 2019 20:44

I stumbled over an exception when one of the items in the dropdownlist ("Data" property) has Null in the property specified as "TextField" on the dropdownControl.

exception/callstack:

Microsoft.AspNetCore.Components.Browser.Rendering.RemoteRenderer: Warning: 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 Telerik.Blazor.Components.DropDownList.TelerikDropDownList`2.<>c__DisplayClass0_0.<BuildRenderTree>b__5(RenderTreeBuilder builder5)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder.AddContent(Int32 sequence, RenderFragment fragment)
   at Telerik.Blazor.Components.DropDownList.ListItem.BuildRenderTree(RenderTreeBuilder builder)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment)
   at Microsoft.AspNetCore.Components.Rendering.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)
   at Microsoft.AspNetCore.Components.Rendering.Renderer.ProcessRenderQueue()
Microsoft.AspNetCore.Components.Server.ComponentHub: Warning: Unhandled Server-Side exception