问题
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