In this article we will discuss Get selected Value of DropDownList in ASP.Net Core MVC.
Here we will explain When the Submit Button is clicked, the selected Value and selected Text of the DropDownList are fetched inside the Controller in ASP.Net Core MVC.
Before start this article, please visit our previous article Using SELECT Tag Helper in ASP.Net Core MVC
Database
Add the package in Entity Framework Core
Model
Add the customer class inside Models folder.
Database Context
Create another class, first inherit the EntityFrameworkCore namespace and then inherit the DbContext class.
Then using Dependency Injection, a Constructor is created DbContextOptions are passed as parameter and also the Constructor of base class i.e. DbContext class is inherited.
Finally, a DbSet Collection property of Customer Model class is created, which will be later used for holding the Data fetched from SQL Server Database Table.
Adding the Connection String inside AppSettings.json
The following Connection String setting has been added in the AppSettings.json file.
Configuring Database Context in Startup class
Inside the Startup class, the IConfiguration is injected in the Startup class and assigned to the private property Configuration.
Then the Connection String is read from the AppSettings.json file and is used to add the DbContext service.
Controller
Inside the Index Action method, we fetched customers data.
View
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
Output
The output like below this.
Post a Comment