C# 无法读取响应数据并将数据添加到我的网格
我正在使用angularjs和web api。我已经从api返回了一个列表,并尝试使用ng grid在网格中显示它们。下面是我的代码。谁能帮帮我吗 设计C# 无法读取响应数据并将数据添加到我的网格,c#,angularjs,asp.net-web-api,C#,Angularjs,Asp.net Web Api,我正在使用angularjs和web api。我已经从api返回了一个列表,并尝试使用ng grid在网格中显示它们。下面是我的代码。谁能帮帮我吗 设计 <html ng-app="myApp"> <head lang="en"> <meta charset="utf-8"> <title>Getting Started With ngGrid Example</title> <script src="https://ajax.g
<html ng-app="myApp">
<head lang="en">
<meta charset="utf-8">
<title>Getting Started With ngGrid Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.2/angular.min.js"></script>
<link href="~/Content/Site.css" rel="stylesheet" />
<link href="~/Content/ng-grid.css" rel="stylesheet" />
<link href="~/Content/ng-grid.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/ng-grid.min.js"></script>
<script src="~/Scripts/custom/cell_edit.js"></script>
</head>
<body ng-controller="MyCtrl">
<div class="col-md-6">
<br /><br />
<div class="col-md-12">
<h4>
<em>*</em> First Name:
</h4>
<input type="text" class="form-control input" name="firstName" ng-model="firstName" required placeholder="Enter first name" /><br />
</div>
<div class="col-md-12">
<h4><em>*</em>Last Name</h4>
<input type="text" class="form-control input" name="lastName" ng-model="lastName" required placeholder="Enter last name"><br /><br />
</div>
<div class="col-md-12" align="left">
<button style="color:white" class="btn btn-primary" ng-click="GetData()">GetEmployees</button>
</div>
<div class="col-md-12" ng-grid="gridOptions" />
</div>
</body>
</html>
JS文件
var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function ($scope, $http) {
var Employees = [];
$scope.GetData = function () {
$http.get('/api/ServerRequest/GetMyData')
.success(function (response) {
console.log(response);
response.forEach(function (item) { Employees.push(item); });
console.log(Employees.length);
})
.error(function (response) {
$scope.ResponseDetails = "response: " + response;
alert("failed..");
});
};
$scope.gridOptions = {
data: 'Employees',
enableCellSelection: true,
enableRowSelection: false,
enableCellEditOnFocus: true,
showColumnMenu: true,
resizable: true,
showSelectionCheckbox: true,
columnDefs: [{ field: 'FirstName', displayName: 'FirstName', enableCellEdit: true },
{ field: 'LastName', displayName: 'LastName', enableCellEdit: true }]
};
}))
雇员阶级
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
请更改
数据:员工,
而不是数据:“员工”
删除
data
paramsI have updated from var Employees=[]上Employees
对象上的单引号;至$scope.Employees=[];无论我在哪里使用过员工,它现在都能正常工作。
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
}