DropDownlist - All options are undefined (Razor Pages)

1 Answer 546 Views
ComboBox DropDownList
Vakho
Top achievements
Rank 1
Vakho asked on 24 Jun 2022, 07:31 PM | edited on 24 Jun 2022, 07:38 PM

So I just created a .NET 6 ASP.NET Core Web App (Razor Pages), added all telerik stuff according to the instructions (NuGet Packages, scripts and styles in _Layout.cshtml, @addTagHelper-s in _ViewImports.cshtml), and HTMLHelpers are rendering correctly. However, when I provide a data source to the DropDownList or ComboBox, all options show up as undefined.

Telerik version: 2022.2.621.   Bootstrap: V5.


Here's the code for DropDownList:

Index.cshtml

@page
@model IndexModel

@inject Microsoft.AspNetCore.Antiforgery.IAntiforgery Xsrf
@Html.AntiForgeryToken()
@{
    ViewData["Title"] = "Home page";
}

--------------------------------------------------------------------------------

 

 

<div class="text-center"> <h1 class="display-4">Welcome</h1> @(Html.Kendo().DropDownList() .Name("MyDropdownn") .DataTextField("Text") .DataValueField("Value") .HtmlAttributes(new { style = "width:300px;" }) .AutoBind(false) .Filter(FilterType.Contains) .DataSource(ds => ds .Custom() .Transport(transport => transport .Read(r => r .Url("/Index?handler=Sports") )) .ServerFiltering(false) ) ) </div>

 

Index.cshtml.cs

        public JsonResult OnGetSports()
        {
            var allSports1 = db.Sports.Where(x => x.SportID > 0).Select(x => new DropDownModel
            {
                Text = x.Name,
                Value = x.SportID 
            }).ToList();

            return new JsonResult(allSports1);
        }

Proof that the data isn't empty/undefined

DropDownModel.cs (DropDownModel class)

    public class DropDownModel
    {
        public int Value { get; set; }
        public string Text { get; set; } = String.Empty;
    }

 

I've tried restarting everything, double-checking everything but nothing helped. Is there a bug with this version of telerik, is something incompatible with ,NET 6 or Bootstrap 5? Or am I doing something wrong? 

1 Answer, 1 is accepted

Sort by
2
Accepted
Alexander
Telerik team
answered on 29 Jun 2022, 03:08 PM

Hi Vakho,

Thank you for reaching out.

The issue that you are facing may be caused by the JSON Serialization configuration that is used for the property name casing of data-bound components within the Telerik UI for ASP.NET Core suite. Having this in mind, if the serializer changes the casing to camelCase, but PascalCase is inspected instead, the component will not bind to the data, resulting in an unsuccessful attempt to display the fetched data.

You can verify this by inspecting the browser's console:

If this is the case, I would recommend setting the JSON Serialization to the initially added services within the integrated "Program.cs" file of the application. For example:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews()
            // Maintain property names during serialization. See:
            // https://github.com/aspnet/Announcements/issues/194
            .AddJsonOptions(options =>
                options.JsonSerializerOptions.PropertyNamingPolicy = null);

// Add Kendo UI services to the services container
builder.Services.AddKendo();

More in-depth information regarding this topic can be found in the following documentation:

If the issue stills persists could you please share if there are any errors thrown within the browser console that could potentially indicate the cause of the issue?

Best Regards,
Alexander
Progress Telerik

The Premier Dev Conference is back! 

Coming to you live from Progress360 in-person or on your own time, DevReach for all. Register Today.


Tags
ComboBox DropDownList
Asked by
Vakho
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Share this question
or