Monday, April 19, 2021

ASP.NET Core HTML helper with DropDownList method

Do the following:

  1. Create ASP.NET Core using SDK version 5.0 template.
  2. Create Models/Employee class as given below inside Models folder:

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.

No comments:

Post a Comment

Hot Topics