Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 8860

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T05:04:09+00:00 2024-11-28T05:04:09+00:00

Razor Pages Add Select option to a Dropdown

  • 60k

Screenshot for two pages

This code sample demonstrates how to add a select option to a dropdown/select element using EF Core and a data provider using SQL-Server NorthWind database using category and contact type tables.

GitHub Source code

For each select, data is read from a database table as follows were the DECLARE is replaced for the data provider Microsoft.Data.SqlClient with a parameter and with EF Core inserted to a list as the first item.

Note the parameter below @SelectText

  DECLARE @SelectText AS NVARCHAR(50) = N'Select';  SELECT -1 AS CategoryID,        @SelectText AS CategoryName UNION ALL SELECT CategoryID,        CategoryName FROM dbo.Categories ORDER BY CategoryName;  SELECT -1 AS ContactTypeIdentifier,        @SelectText AS ContactTitle UNION ALL SELECT ContactTypeIdentifier,        ContactTitle FROM dbo.ContactType ORDER BY ContactTitle;   
Enter fullscreen mode Exit fullscreen mode

Then for the data provider after data is read into a list the following provides a custom order by which starts with -1 which represents the item to have a user make a selection.

  var identities = Enumerable.Range(-1, count).ToList();   
Enter fullscreen mode Exit fullscreen mode

Data is read then ordered using the above list.

  list.OrderBy(c => identities.IndexOf(c.CategoryID)).ToList();   
Enter fullscreen mode Exit fullscreen mode

Select text

The select text is stored in appsettings.json, in this case one for each page.

  {   "Logging": {     "LogLevel": {       "Default": "Information",       "Microsoft.AspNetCore": "Warning",       "Microsoft.EntityFrameworkCore.Database.Command": "Warning"     }   },   "AllowedHosts": "*",    "ApplicationFeatures": {      "IndexPage": {       "SelectText": "Select"     },     "Index1Page": {       "SelectText": "Select"     }   },   "ConnectionStrings": {     "DefaultConnection": "Data Source=.\SQLEXPRESS;Initial Catalog=Northwind2020;Integrated Security=True;Encrypt=False"   } }   
Enter fullscreen mode Exit fullscreen mode

Model for above

  public class ApplicationFeatures {     /// <summary>     /// Index page section in appsettings.json     /// </summary>     public const string Index = "ApplicationFeatures:IndexPage";     /// <summary>     /// Index1 page section in appsettings.json     /// </summary>     public const string Index1 = "ApplicationFeatures:Index1Page";     public string SelectText { get; set; } }   
Enter fullscreen mode Exit fullscreen mode

  • Two constants point to sections in appsettings.json
  • SelectText property points to text used for the first item in each dropdown.

Index page

This page uses EF Core for populating the select element.

  1. In the page constructor gets the selection text from appsettings
  2. Reads data from the Categories table with an order by on CategoryName
  3. Inserts select text into the list
  4. Creates a List<SelectListItem> for the list
  5. Then in the frontend create the select element set to the SelectListItem and name set to the primary key, in this case CategoryID which on post will have the selected item.
  <label asp-for="SelectedCategory">     Categories     <select name="CategoryID"             asp-for="SelectedCategory"             class="form-select"             style="width: 9.2em"             asp-items="Model.Options">     </select> </label>   
Enter fullscreen mode Exit fullscreen mode

Index1 page

This page uses the data provider for populating the select element.

  1. In the page constructor gets the selection text from appsettings
  2. Reads data from the Categories table with an order by on CategoryName and add the select option from the class ReferenceTableOperations
  3. Inserts select text into the list
  4. Creates a List<SelectListItem> for the list
  5. Then in the frontend create the select element set to the SelectListItem and name set to the primary key, in this case CategoryID which on post will have the selected item.
  <label asp-for="SelectedCategory">    Categories    <select name="CategoryID"       asp-for="SelectedCategory"       class="form-select"       style="width: 9.2em"       asp-items="Model.Options">    </select> </label>   
Enter fullscreen mode Exit fullscreen mode

Both pages select element

Although both pages load the data differently, they both use the same HTML configuration.

Try it out

Index2 page is ready for you the reader to try out where all setup is done for EF Core. Following along with Index Page which presents Categories and finish this page with ContactType.

  1. Copy code from Index.cshtml starting with the style section
  2. Paste into Index2.cshtml (at this point disrerard any errors)
  3. Copy code from Index.cshtml.cs, from the open class bracket to the end class bracket
  4. Replace Index2.cshtml.cs with the above
  5. Now alter code from categories to contact type.

Note for Index2 page and the other pages

Configuration has already been done in Program.cs for reading the select text.

  private static void ApplicationConfigurations(WebApplicationBuilder builder) {     builder.Services.Configure<ApplicationFeatures>(ApplicationFeatures.Index,         builder.Configuration.GetSection(ApplicationFeatures.Index));      builder.Services.Configure<ApplicationFeatures>(ApplicationFeatures.Index1,         builder.Configuration.GetSection(ApplicationFeatures.Index1));      builder.Services.Configure<ApplicationFeatures>(ApplicationFeatures.Index2,         builder.Configuration.GetSection(ApplicationFeatures.Index2)); }   
Enter fullscreen mode Exit fullscreen mode

To learn more about reading values from appsettings.json see Storing and reading values from appsettings.json

Source code

Clone the following GitHub repository.

csharplearningsoftwaredevelopmentwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.