DropDownList "Initial value" in ASP.NET MVC

1 Answer 68 Views
DropDownList
Maik
Top achievements
Rank 1
Maik asked on 04 Feb 2025, 11:18 AM

am using Kendo UI with Razor as the frontend and .NET Framework 4.8.1 as the backend.
I have the following code, but it does not initially display "Switzerland".
It is present in the list, but I do not want to select it manually.
I want it to be preselected from the start. Could you please help me?


@model int?

@{
    var initialItems = new List<SelectListItem>()
{
        new SelectListItem{ Text = "Schweiz", Value = "1" }
    };
}

<div class="k-floating-label-container mb-3">
    @(Html.Kendo().DropDownListFor(x => x)
        .DataTextField("Text")
        .DataValueField("Value")
        .AutoBind(false)
        .BindTo(initialItems)
        .Value(Model?.ToString())
        .Events(e => e.Open("onDropDownOpen"))
        .Deferred()
    )
    @Html.LabelFor(x => x, new { @class = "k-label k-input-label" })
    @Html.ValidationMessageFor(x => x)
</div>

<script>function onDropDownOpen(e) {
    var dropdown = $("#Store_DefaultLanguageId").data("kendoDropDownList");

    if (dropdown.dataSource.total() === 1) { 
        dropdown.setDataSource(new kendo.data.DataSource({
            transport: {
                read: {
                    url: "/Language/LanguageList",
                    dataType: "json",
                }
            },
            serverFiltering: true
        }));
    }
}
</script>

1 Answer, 1 is accepted

Sort by
0
Anton Mironov
Telerik team
answered on 07 Feb 2025, 08:01 AM

Hello Maik,

Thank you for the code snippet and the details provided.

In order to set an initial value for the DropDownList, I would recommend using the Value Configuration:

Or to set it in the document.ready scope with the help of the value method:

I hope this information helps.

Kind Regards,
Anton Mironov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
DropDownList
Asked by
Maik
Top achievements
Rank 1
Answers by
Anton Mironov
Telerik team
Share this question
or