How to Add A dropdown List In ef core Razor Pages without creating A seperate Database For Dropdown Values

Arnab 66 Reputation points
2022-07-12T05:47:27.363+00:00

I want to make A dropdown List While Creating A form .After Choosing the particular field will be saved in the database But There will not be any separate Database for Dropdown Inputs previously . I want to Hard code it. Using Code first Migration.
my class

 public class Ittracker   
    {  
        //[Required]  
        //[RegularExpression("([a-zA-Z][a-zA-Z ]+)", ErrorMessage = "Only alphabets are allowed")]  
        public string Name { get; set; }  
         
        public int Id { get; set; }  
  
  
         
        public string EmailId { get; set; }  
  
       
        public string SupportType { get; set; }  
  
  
        public string Description { get; set; }  
        public string Requeststatus { get; set; }  
  
        public string caseDetails { get; set; }  
  
 
        
    }  

So i want to make the Requeststatus Like below exmample but dont want to create a seperate database so how should I approach plz suggest.

219725-screenshot-2022-07-12-111146.jpg

Developer technologies .NET Entity Framework Core
Developer technologies ASP.NET ASP.NET Core
{count} votes

1 answer

Sort by: Most helpful
  1. Yiyi You - MSFT 521 Reputation points
    2022-07-12T07:40:06.607+00:00

    Hi,@Arnab

    As you said,you need to hard code it.But it is hard to use Code first Migration to add a dropdown like this by default,so you can try to add the following code to your page.

    <select asp-for="Requeststatus">  
        <option value="Active">Active</option>  
        <option value="In Progress">In Progress</option>  
        <option value="Resolved">Resolved</option>  
        <option value="Closed">Closed</option>  
        <option value="Waiting">Waiting</option>  
    </select>  
    

    Or you can try to set List<SelectListItem> type data in cshtml.cs file,and use asp-items in the dropdown:

    [BindProperty]  
            public List<SelectListItem> RequeststatusList { get; set; }  
            public void OnGet()  
            {  
                List<string> l = new List<string> { "Active", "In Progress", "Resolved", "Closed", "Waiting" };  
                RequeststatusList = l.Select(x => new SelectListItem { Text = x, Value = x }).ToList();  
            }  
    

    cshtml:

    <select asp-for="Requeststatus" asp-items="@Model.RequeststatusList"></select>  
    

    ----------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Yiyi You

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.