DropDownList With Two Hardcoded Items using TagHelpers?

1 Answer 993 Views
DropDownList
Russell
Top achievements
Rank 1
Iron
Iron
Iron
Russell asked on 01 Oct 2021, 06:11 AM
I don't want to have to round trip for two items? How do I do that in TagHelpers?

1 Answer, 1 is accepted

Sort by
0
Accepted
Aleksandar
Telerik team
answered on 01 Oct 2021, 11:07 AM | edited on 06 Oct 2021, 09:00 AM

Hi Russell,

To create a DropDownList with two options using TagHelpers without setting a DataSource configuration you can, for example, use the following definition in the View:

@{
    var data = new List<SelectListItem>() { new SelectListItem { Text = "Male", Value = "1" }, new SelectListItem { Text = "Female", Value = "2" } };
}

<kendo-dropdownlist name="sex" datatextfield="Text" datavaluefield="Value" bind-to="data" on-change="onChange">
</kendo-dropdownlist> <script> function onChange(e) { alert(e.sender.dataItem().Text) } </script>

 

You can see the above in action in this screencast. The example demonstrates how you can also attach a change event handler. For details on the TagHelper configuration refer to the DropDownList TagHelper section of the documentation.

Regards,
Aleksandar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Russell
Top achievements
Rank 1
Iron
Iron
Iron
commented on 01 Oct 2021, 08:28 PM

Hi, 

Thanks for responding. I was rushing too much and I apologize.

My request is really basic: I need a Sex drop down with "Male" and "Female". I was hoping for a simple hard coded mechanism to add those two items - via taghelpers - to a dropdownlist?

Aleksandar
Telerik team
commented on 06 Oct 2021, 09:00 AM

Thank you for the additional details. I updated the initial answer with an example based on the details provided.
Russell
Top achievements
Rank 1
Iron
Iron
Iron
commented on 12 Oct 2021, 05:45 AM

Perfect, thank you!
Tags
DropDownList
Asked by
Russell
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Aleksandar
Telerik team
Share this question
or