2015-06-30 58 views
-1

我有這個在國家MVC控制器從哪裏獲得的國家名單:如何從MVC控制器的角度填充下拉列表?

model.Edit.Countries = new SelectList(manager.GetCountries(), "Id", "Name"); 

我的問題是我怎麼能發送從角請求得到這個國家在$範圍,然後向他們展示在下拉列表中?

回答

0

創建的WebAPI控制器

namespace Example.api 
{ 
    public class ExampleController : ApiController 
    { 

     ...... 

     [HttpGet] 
     public List<Countries> GetAll() 
     { 
      return new SelectList(manager.GetCountries(), "Id", "Name"); 
     } 

     ...... 

    } 
} 

AngularJS創建service獲取數據:

app.factory('countryFactory', [ 
    '$http', function($http) { 
     var baseAddress = '/Example/'; // path to your controller 
     var url; 
     return { 
      getAll: function() { 
       url = baseAddress + 'GetAll/'; 
       return $http.get(url); 
      } 
     } 
    } 
]); 

使用它在controller

app.controller('controllerName', ['$scope', 'countryFactory', function ($scope, countryFactory) { 

    ...... 

    countryFactory.getAll().success(function(data) { 
     $scope.countries = data  // this is the $scope variable with `countries` list 
    }).error(function() { 

    }) 

    ...... 

}])