Skip to content

Adding attributes and classes in TagHelper DropDownList causes NullReferenceException when runtime compilation is enabled #8068

@kendo-bot

Description

@kendo-bot

Bug report

When the project is configured for runtime compilation, and the TagHelper DropDownList has custom classes or attributes, NullReferenceException is thrown.

Reproduction of the problem

  1. Create a .NET 8 project and install the Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation NuGet package.

  2. Define a TagHelper DropDownList and add a custom class:

//Program.cs
builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation();

builder.Services.AddMvc().AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
builder.Services.AddKendo();

//View

<kendo-dropdownlist for="Id" datatextfield="Text" datavaluefield="Value" auto-width="true" class="form-control w-75">
    <datasource type="Kendo.Mvc.UI.DataSourceTagHelperType.Ajax">
        <transport>
            <read url="@Url.Action("GetData", "Home")"/>
        </transport>
    </datasource>
</kendo-dropdownlist>
  1. Run the application and observe the exception: "NullReferenceException: Object reference not set to an instance of an object."

  2. When using the HtmlHelper version of the DropDownList or removing the custom class from the TagHelper declaration, the error does not occur.

Expected/desired behavior

The TagHelper DropDownList must exhibit the same behavior as the HtmlHelper DropDownList.

Workaround

Add the custom class with jQuery:

<kendo-dropdownlist for="Id" datatextfield="Name"  datavaluefield="Id"  auto-width="true">
 ...
</kendo-dropdownlist>

<script>
$(document).ready(function(){
    $("#Id").addClass("form-control w-75"); // add the class to the hidden input element
    $("#Id").attr("required", "required");
    $("#Id").closest("span.k-dropdownlist").addClass("form-control w-75"); // add the class to the wrapper element
});
</script>

Environment

  • **Telerik UI for ASP.NET Core version: 2024.4.1112
  • **Browser: [all]

Metadata

Metadata

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions