How do I populate dropdownlist inside Razor pages Not MVC

ladd4luv_22 1 Reputation point
2022-07-20T11:39:18.067+00:00

How do I populate dropdownlist inside Razor pages Not MVC because
I have the code working fine in .Net core MVC but not working in .Net core Razor Page
How do I display the list in dropdown with asp-items="????????" i mean

How do I display from here

public IActionResult Droplist()  
   {  
        List<ListMode> _Define = PopulateList();  
        return View(new SelectList(_Define, "PredefineID", "Predefine"));  
    }  

return View(……); in above code is not working in Razor page

In side my Razor page I have this.

@Page  
@model…  
<form method="post">  
<select id="ddlFruits" name="PredefineID" asp-items="????????">  
<option value="0">Please select</option>  
</select>  
</form>  
                                  

Other part of the code
Database: dbo.DropListTBL

PredefineID Predefine
1 Class
2 Subject
3 Option
4 Status

Model Class:

public class ListMode  
{  
    public int PredefineID { get; set; }  
    public string Predefine { get; set; }  
}  

Also:

private static List<ListMode> PopulateList()  
    {  
        string constr = @"Data Source =.; initial Catalog = DBname; Integrated Security = True";  
        List<ListMode> List_Mode = new List<ListMode>();  
        using (SqlConnection con = new SqlConnection(constr))  
        {  
            string query = "SELECT PredefineID, Predefine FROM dbo.DropListTBL";  
            using (SqlCommand cmd = new SqlCommand(query))  
            {  
                cmd.Connection = con;  
                con.Open();  
                using (SqlDataReader sdr = cmd.ExecuteReader())  
                {  
                    while (sdr.Read())  
                    {  
                        List_Mode.Add(new ListMode  
                        {  
                            Predefine = sdr["Predefine"].ToString(),  
                            PredefineID = Convert.ToInt32(sdr["PredefineID"])  
                        });  
                    }  
                }  
                con.Close();  
            }  
        }  

        return List_Mode;  
    }  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,156 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,252 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,601 Reputation points
    2022-07-20T16:08:52.863+00:00
    @Page  
    @model…  
    @{  
        var list = new SelectList(PopulateList(), "PredefineID", "Predefine"));  
    }  
     <form method="post">  
       <select id="ddlFruits" name="PredefineID" asp-items="list">  
            <option value="0">Please select</option>  
       </select>  
     </form>  
    

    you really should use async sql and put the init code OnGetAsync, and make the list part of the model

     @Page  
     @model…  
         <form method="post">  
           <select id="ddlFruits" name="PredefineID" asp-items="Model.FruitList">  
                <option value="0">Please select</option>  
           </select>  
         </form>  
    

    and in PageModel:

    public List<SelectListItem> FruitList {get; set;} = new  List<SelectListItem>;  
      
    public async Task OnGetAsync()  
    {  
       var data = await PopulateListAsync();  
       var list = new SelectList(data, "PredefineID", "Predefine"));  
    }  
    

    note: you should read the basic intro docs

    0 comments No comments

  2. Ruikai Feng - MSFT 2,526 Reputation points Microsoft Vendor
    2022-07-21T19:36:39.847+00:00

    Hi,@ladd4luv_22

    I tried as below and it could work:
    in pagemodel:

    public class TestModel : PageModel  
        {  
            public SelectList DropDownList { get; set; }  
      
            public void OnGet()  
            {  
                var List_Mode = GetDropDownList();  
                DropDownList = new SelectList(List_Mode, "PredefineID", "Predefine");  
            }  
      
            //assuming you get the list successfully from db,I tried with hard coding  
            public List<ListMode> GetDropDownList()  
            {  
                var List_Mode = new List<ListMode>()   
                {  
                new ListMode(){PredefineID=1,Predefine="Class"},  
                new ListMode(){PredefineID=2,Predefine="Subject"},  
                new ListMode(){PredefineID=3,Predefine="Option"},  
                new ListMode(){PredefineID=4,Predefine="Status"}  
                };  
                return List_Mode;  
            }  
        }  
    

    in page:

    @page  
    @model RazorPage7._22.Pages.TestModel  
      
    <form method="post">  
        <select id="ddlFruits" name="PredefineID" asp-items="@Model.DropDownList">  
            <option value="0">Please select</option>  
        </select>  
    </form>  
    

    Result:

    223351-2022-07-22-034213.png

    ----------

    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
    RuikaiFeng

    0 comments No comments