这是我的模型文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ConsumeJson.Models
{
public class ProductModel
{
public List<Product> findAll()
{
List<Product> result = new List<Product>();
result.Add(new Product { Id = "p01", Name = "Product 1", Price = "100", Quantity = "1" });
result.Add(new Product { Id = "p02", Name = "Product 2", Price = "200", Quantity = "2" });
result.Add(new Product { Id = "p03", Name = "Product 3", Price = "300", Quantity = "3" });
return result;
}
public Product find(string id)
{
return findAll().Single(p => p.Id.Equals(id));
}
}
}
这是我的 HTML 文件。
<!DOCTYPE html>
<html ng-app="myapp">
<head>
<meta charset="utf-8" />
<script src="Scripts/jquery-3.3.1.min.js"></script>
<script src="Scripts/jquery-3.3.1.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></script>
<title>My Client</title>
</head>
<body ng-controller="productController">
<table cellpadding="2" cellspacing="2" border="1">
<tr>
<th>Id</th>
<th>Name</th>
</tr>
<tr ng-repeat="product in result">
<td>{{product.Id}}</td>
<td>{{product.Name}}</td>
</tr>
</table>
<script type="text/javascript">
var myapp = angular.module('myapp', []);
myapp.controller('productController', function ($scope, $http) {
$http(
method: 'GET',
url:'http://localhost:53204/api/product').success(function (response) {
$scope.result = response;
})
})
</script>
</body>
</html>
我想创建包含信息的产品表,但是当我作为本地主机运行它时,它永远不会显示产品的 ID 或名称。
它保持这样。 {{Product.Id}}
{{Product.Name}}
我该如何解决这个问题?
最佳答案
您需要将其更改为使用.then()
$http({
method: "GET",
url: "http://localhost:53204/api/product"
}).then(function mySucces(response) {
$scope.result = response.data;
}, function myError(response) {
});
关于javascript - C# Web Api 获取方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51460898/