In this article, we will learn how to create and bind Dropdown List using ViewBag in ASP.NET Core MVC. Let's learn step by step by creating an ASP.NET Core application.
- Start then All Programs and select "Microsoft Visual Studio 2019".
- Once the Visual Studio Opens, Then click on Continue Without Code.
- Then Go to Visual Studio Menu, click on File => New Project then choose ASP.NET Core Web App (Model-View-Controller) Project Template.
- Then define the project name, location of the project, Target Framework, then click on the create button.
The preceding steps will create the ASP.NET Core MVC application. If you are new to the ASP.NET core and know how to create an ASP.NET Core application in depth, then refer to my following article.
Step 2: Add controller class
Delete the existing controller for ease of understanding, then add a new controller by right clicking on the Controller folder in the created ASP.NET Core MVC application, give the Controller name Home or as you wish as in the following.
Now open the HomeController.cs file and write the following code into the Home controller class as in the following.
Step 3: Add View
Add an empty view named Index.cshtml and write the following code.
Step 4: Run the Application.
Now press Keyboard F5 or Visual Studio run button to run the application. After running the application, the output will be shown on the browser, as in the following screenshot.
I hope, from all the above examples, you have learned how to bind the DropDownList using ViewBag in ASP.NET Core MVC.