Bind DropDownList from Database in ASP.Net Core MVC

AhmedB
 
on Oct 11, 2021 05:16 AM
349 Views

Hello Experts I am seeking your guidance to help with what I believe is simple but still have no knowledge of how it is done I have a very simple web application with only 3 pages

The main page is a registration page that has a dropdown to select Job Category, I have the following code in the Controller

public enum JobCategory
{
    Agriculture,
    Distribution,
    Education,
    Manufacturing,
    Security,
    Transport,
    Warehousing,
    Other
}

And in the View, I have the following code

<div class="form-group">
     <label asp-for="JobCategory" class="control-label">Job Category</label>
     @Html.DropDownListFor(m => m.JobCategory, new SelectList(Enum.GetNames(typeof(JobCategory))), "Select Job Category", new { @class = "form-control" })
</div>

The problem I am having is that I cannot use SPACES in the ENUM list items as values, for example, "Food Industry" I actually believe that using ENUM for dropdown list is not the best way, I prefer to use an SQL table to populate the Job Categories from that table into a dropdown list in the view for the user to select from

Can you please guide me to either, how I modify what I have currently of USING ENUM to get the list but I need to use the DESCRIPTION instead? Or better instead of using ENUM, how to populate the dropdown list items from JobCayegories SQL table, the application is already connected to the SQL DB via ConnectionStrings

Cheers

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 11, 2021 05:24 AM