Monday, April 19, 2021

ASP.NET Core HTML helper with DropDownList method

Create ASP.NET Core 5.0 based application.

ASP.NET Core Html.DropDownList method simple example

Create Models/Employee class

namespace LINQArray.Models
{
    public class Employee
    {        
    }
    public enum Gender
    {
        Male,
        Female
    }
}

In the HomeController, the Index action is as follows.

using Microsoft.AspNetCore.Mvc;

namespace LINQArray.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {

            return View();
        }
    }
    
}

In the Index view, the code is as follows.

@using LINQArray.Models

<div>Dropdown Example</div><hr/>
<div>
    @Html.DropDownList("ddl", new SelectList(Enum.GetValues(typeof(Gender))))
</div>

Run the application. We get the dropdown with Gender items.


Updated on 6 July 2023

No comments:

Post a Comment

Hot Topics