Why getting value like this [1] in AngularJS

冷暖自知 提交于 2019-12-24 18:19:32

问题


I need to get id from table but I am getting id like this [1]. I need to get 1 only.

Controller

 public JsonResult GetLineManagerId(string LineManagerId)
        {
var lineManagerId = db.Employees
       .Where(x => x.FirstName == LineManagerId)
       .Select(x =>x.Id);
            return Json(lineManagerId);
        }

Angular

$scope.GetLineManagerId = function (LineManagerId) {
            $http.get('/Official/GetLineManagerId?LineManagerId=' + LineManagerId).then(function (data) {
                console.log(data);
                $scope.lineManagerId = data.data;
                $scope.empModel.lineManagerId = $scope.lineManagerId[0].lineManagerId;

            });
        };

html

 <select ng-model="empModel.name" id="" name="Name" class="form-control" 
                                    ng-change="GetLineManagerId(empModel.name)"
                                    ng-click="GetLineManagerId(empModel.name)"
                                    ng-options="d.name as d.name for d in name">
                                <option></option>


     </select> 
  <select ng-model="empModel.id" id="" name="Id" class="form-control"
                                  >
                                <option>{{lineManagerId}}</option>
                            </select>  

when I click on 1st dropdown I get id like this [1].. I need to get number only like this 1

I need to get id number in dropdown, also if someone help me to get id in text input instead of using dropdown.


回答1:


Try this, Please don't use ng-click here

 <select ng-model="empModel.name" id="" name="Name" class="form-control" 
                                    ng-change="GetLineManagerId(empModel.name)"
                                    ng-options="d.name as d.name for d in name">
                                <option></option>

Load linemanager using this

  <select ng-model="empModel.id" id="" name="Id" class="form-control"
                                ng-options="d.lineManagerId as d.lineManagerId for d in lineManagerId"
                                  >
                             <option></option>   
                            </select>  

your other portion will remain unchanged I think




回答2:


You are returning an array/enumeration instead of a single value.

public JsonResult GetLineManagerId(string LineManagerId)
{
    var lineManagerId = db.Employees
       .Where(x => x.FirstName == LineManagerId)
       .Select(x =>x.Id); // returns an IEnumerable<T> for whatever Id is

    return Json(lineManagerId);
}

You have to select a single result like

var lineManagerId = db.Employees
   .Where(x => x.FirstName == LineManagerId)
   .Select(x =>x.Id)
   // returns first value or default, i.e. 0 for int, null for string
   .FirstOrDefault();


来源:https://stackoverflow.com/questions/48736223/why-getting-value-like-this-1-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!