Categories : ASP.NET Core
Author : Date : Jan 31, 2022
Tags : asp.net core, Dependency Injection, Dropdown, Services
This blog explains how to load a drop-down list in View using a dependency injection, without passing the value from the controller. So that it will reduce the request to the controller. Let us see how to achieve this.
In this blog I have explained two types of drop-down list loading methods. The first one is a list of classes, the second one is a list of strings. The following are Metals and OptionServices classes. The OptionServices provide a list of colors and a list of metal information (metal name and symbol)
To use the service, you must register the service in the ConfigureServices method in the Startup.cs file.
The one below is the view code. Here I have injected the OptionServices into view and loaded it into the dropdown list
PopulateDropDown.cshtml
The following is a controller code. We do not need to pass any values here to load the drop-down list
The following is an output of the above code.
This article explains how to load the dropdown using service with a small example.
If you have any questions about this please leave a comment below.