Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to use select list asp-items in asp.net core?


Asked by Musa Hicks on Nov 29, 2021 ASP.NET



The the Select List asp-items Tag Helper is used for populating DropDownList in ASP.Net Core Razor Pages. In this article I will explain with an example, how to use the Select List asp-items Tag Helper in ASP.Net Core Razor Pages. The the Select List asp-items Tag Helper is used for populating DropDownList in ASP.Net Core Razor Pages.
One may also ask,
If you want to set an option as the selected option, you may simply set the SelectedCategory property value.
Keeping this in consideration, The Select Tag Helper asp-for specifies the model property name for the select element and asp-items specifies the option elements. For example: The Index method initializes the CountryViewModel, sets the selected country and passes it to the Index view. We don't recommend using ViewBag or ViewData with the Select Tag Helper.
In respect to this,
The <datalist> element can be used to create a HTML-only "type-to-select" drop-down list (aka combobox with input enabled). The elements with the <datalist> tag are of type <option>, just like those that would be in a <select> list, which is supported by the asp-items Razor tag helper.
In addition,
Assuming context is your db context object. The view code is same as above. Some people prefer to use SelectList class to hold the items needed to render the options. Now in your GET action, you can use the SelectList constructor to populate the Employees property of the view model.