简体   繁体   English

从JavaScript的控制器中将数据列表获取到HTML select中

[英]Get list of data into HTML select from a controller in JavaScript

I'm trying to get data into HTML select with this code: 我正在尝试使用此代码将数据导入HTML select:

HTML: HTML:

<div ng-controller="Manufacturer">
    <select ng-model="myManufacturer" ng-options="Manufacturer.name for Manufacturer in Manufacturers" class="form-control"></select>
</div>

JS: JS:

angular.module('MyApp')

    .controller('Manufacturer', ['$scope', function ($scope) {
        $scope.CarManufacturer = null;
        $.post("/Data/GetAllManufacturers");
        $scope.Manufacturers = $.post.Data;
        alert($scope.CarManufacturer);
      //  $scope.Manufacturers = [
      //  { name: 'Audi' , id: 1 },
      //  { name: 'Volvo', id: 2 },
      //  { name: 'BMW', id: 3 },
      //  { name: 'Skoda', id: 4 },
      //  { name: 'Siat', id: 5 }
      //];
        $scope.myManufacturer = $scope.Manufacturers[1]; // red
    }]);

CONTROLLER: 控制器:

public List<string> GetAllManufacturers()
{
    dbCarEntities us = new dbCarEntities();
    List<string> asd = us.CarManufacturers.Select(x => x.Name).ToList();
    return asd;
}

Explanation: 说明:

I have an HTML select. 我有一个HTML选择。 I want to fill it with a list from my SQL table with Model Entity framework. 我想用模型实体框架的SQL表中的列表填充它。 So I need to go to a function and get back the list. 因此,我需要转到一个函数并返回列表。 I tried also with JSON. 我也尝试了JSON。 It doesn't work either. 它也不起作用。

got it!! 得到它了!!

angular.module('MyApp') // extending from previously created angularJS  module in the First part
.controller('Part5Controller', function ($scope, LocationService) {
    // expained about controller in Part2 // Here LocationService (Service) Injected

    $scope.CountryID = null;
    $scope.StateID = null;
    $scope.CountryList = null;
    $scope.StateList = null;

    $scope.StateTextToShow = "Select State";
    $scope.Result = "";

    // Populate Country
    LocationService.GetCountry().then(function (d) {
        $scope.CountryList = d.data;
    }, function (error) {
        alert('Error!');
    });
    // Function For Populate State  // This function we will call after select change country
    $scope.GetState = function () {
        $scope.StateID = null; // Clear Selected State if any
        $scope.StateList = null; // Clear previously loaded state list
        $scope.StateTextToShow = "Please Wait..."; // this will show until load states from database

        //Load State 
        LocationService.GetState($scope.CountryID).then(function (d) {
            $scope.StateList = d.data;
            $scope.StateTextToShow = "Select State";
        }, function (error) {
            alert('Error!');
        });

    }
    // Function for show result
    $scope.ShowResult = function () {
        $scope.Result = "Selected Country ID : " + $scope.CountryID + " State ID : " + $scope.StateID;
    }

})
.factory('LocationService', function ($http) { // explained about factory in Part2
    var fac = {};
    fac.GetCountry = function () {
        return $http.get('/Data/GetCountries')
    }
    fac.GetState = function (countryID) {
        return $http.get('/Data/GetStates?countryID='+countryID)
    }

    return fac;
});

in $http.get we can put how many arguments we want. 在$ http.get中,我们可以输入所需的参数个数。

in the controller : 在控制器中:

public JsonResult GetCountries()
{
    List<Country> allCountry = new List<Country>();
    using (MyDatabaseEntities dc = new MyDatabaseEntities())
    {
        allCountry = dc.Countries.OrderBy(a => a.CountryName).ToList();
    }
    return new JsonResult { Data = allCountry, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
// Fetch State by Country ID

public JsonResult GetStates(int countryID)
{
    List<State> allState = new List<State>();
    using (MyDatabaseEntities dc  = new MyDatabaseEntities())
    {
        allState = dc.States.Where(a => a.CountryID.Equals(countryID)).OrderBy(a => a.StateName).ToList();
    }
    return new JsonResult { Data = allState, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

and finally HTML view: 最后是HTML视图:

<div ng-controller="Part5Controller">
    Country : <select ng-model="CountryID" ng-options="I.CountryID as I.CountryName for I in CountryList" ng-change="GetState()">
                <option value="">Select Country</option>
              </select>
    State : <select ng-model="StateID" ng-options="I.StateID as I.StateName for I in StateList">
                <option value="">{{StateTextToShow}}</option>
            </select>
    <input type="button" value="Get Selected Values" ng-click="ShowResult()"/>
    <div style="padding:10px; font-weight:bold; border:1px solid #f3f3f3">
        {{Result}}
    </div>
</div>

@section scripts{
    <script src="~/Scripts/AngularController/Part5Controller.js"></script>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示从Javascript到Select Statement HTML的数据列表? - How to display a list of data from Javascript into a Select Statement HTML? ASP .NET Core MVC-将HTML选择控件中的列表数据发布到MVC控制器中 - ASP .NET Core MVC - Post List data from HTML select control into MVC Controller 从Controller接收JSON数据后在javascript中生成选择列表选项 - Generate select list options in javascript after recevinig JSON data from Controller 如何从Spring Controller到JavaScript获取列表 - How to get list from spring controller to JavaScript Javascript从html标记中选择特定的数据 - Javascript select specificed data from html tag 如何从选择列表中的html5数据属性获取数据到数组中? - how to get data from html5 data attribute, which is in a select list, into an array? Javascript - 从选择列表中获取文本值 - Javascript - Get text value from select list javascript-从选择列表中获取选择的值 - javascript - get selected value from select list 使用php从mysql中选择数据,显示在html下拉列表中,并通过javascript插入所选值作为URL参数 - Select data from mysql with php, display in html dropdown list and insert selected value as URL parameter via javascript 尝试从javascript中的django获取列表列表作为csv的数据,但是引号以html编号显示 - trying to get list of list from django in javascript as data for csv but quotes shown in html number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM