In development, sometimes we have to populate the data into the dropdownlist. Today, in this blog, we will learn how to bind the dropdownlist, using WebAPI and AngularJS. Thus, let's take a look.

Step 1

Open Visual Studio.

Step 2

File>>New>> Project

Choose ASP.NET WebApplication.

Visual Studio

Step 3

Select WebAPI.

Visual Studio

Click OK button.

Check Solution Explorer.

Visual Studio

In this example, we have one StudentDB database, where we have the students information. First, create StudentDB and one table StateMaster.

Visual Studio

In this example, we will bind StateName as the text and StateId as the value into the dropdownlist.

Click Add button. Now on the basis of wizard, create an edmx file.

Now, open HomeController. By default, HomeController will be inheritted by Controller as HomeController:Controller. Change it to HomeController:ApiController and copy the code, given below.

  1. using AngularwithWebAPI.DbContext;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Http; //This namespace is responsible for IHttpActionResult
  7. using System.Web.Mvc;
  8. namespace AngularwithWebAPI.Controllers
  9. {
  10. publicclassHomeController: ApiController {
  11. DbContext.StudentEntities studentEntities = new DbContext.StudentEntities();
  12. publicIHttpActionResult GetStates() {
  13. var states = studentEntities.StateMasters.ToList();
  14. return Ok(states);
  15. }
  16. }
Now run your Service



Script.js

  1. var
    testApp = angular.module("testModule", []).controller("testController", function($scope, $http) {
  2. $http.get('http://localhost:50623/api/home/getstates').then(function(response) {
  3. $scope.states = response.data;
  4. });
  5. });
index.html
  1. <!DOCTYPEhtml>
  2. <htmlng-apphtmlng-app="testModule">
  3. <head>
  4. <scriptsrcscriptsrc="Scripts/angular.min.js">
  5. </script>
  6. <scriptsrcscriptsrc="Scripts/js/Script.js">
  7. </script>
  8. <title></title>
  9. <metacharsetmetacharset="utf-8" /> </head>
  10. <body>
  11. <divng-controllerdivng-controller="testController">
  12. <tablebordertableborder="1">
  13. <tr>
  14. <td> Name </td>
  15. <td>
  16. <inputtypeinputtype="text" id="txtName" ng-model="name" />
  17. </td>
  18. <tr>
  19. <td> State </td>
  20. <td> <select>
  21. <optionng-modeloptionng-model="stateId"data-ng-repeat="st in states"value="{{st.StateId}}">{{st.StateName}}</option>
  22. </select> </td>
  23. </tr>
  24. <tr>
  25. <td> City </td>
  26. <td>
  27. <inputtypeinputtype="text" id="txtCity" /> </td>
  28. </tr>
  29. <tr>
  30. <td> Address </td>
  31. <td>
  32. <inputtypeinputtype="text" id="txtAddress" /> </td>
  33. </tr>
  34. <tr>
  35. <td> Phone </td>
  36. <td>
  37. <inputtypeinputtype="text" id="txtPhone" /> </td>
  38. </tr>
  39. <tr>
  40. <td>
  41. <inputtypeinputtype="button" id="bttnUpdate" value="Update" /> </td>
  42. </tr>
  43. </table>
  44. </div>
  45. </body>
  46. </html>
In the code, used above, I used data-ng-repeat, which will convert option tag row by the row of StateName. Now, one question arises, why do I use data-ng-repeat="st in states" because, I will return the data into my scope model which is $scope.states.
  1. <select>
  2. <optionng-model="stateId"data-ng-repeat="st in states"value="{{st.StateId}}">{{st.StateName}}</option>
  3. </select>
Now, time to check the output.

Visual Studio

Thus, we can see the list of states in the DropDownList now.
You can watch this blog in Video mode too.