【发布时间】:2015-11-01 15:19:24
【问题描述】:
我试图弄清楚为什么我的 JSON 响应被方括号括起来。我正在使用 ASP.NET Web API 和 Angular。我在想这就是我的 Angular 代码没有打印到 HTML 的原因。
namespace GeekQuiz.Controllers
{
public class TrialController : ApiController
{
private TriviaContext db = new TriviaContext();
// GET api/trial
public IQueryable<TriviaQuestion> GetTriviaQuestions()
{
return db.TriviaQuestions;
}
}
}
上面的代码来自我的TrialController,我正在从 Angular 打个电话。
var app = angular.module('myApp', []);
app.controller('questCtrl', function ($scope, $http) {
$http.get("/api/trial").success(function (data, status, headers, config) {
$scope.options = data.options;
}).error(function (data, status, headers, config) {
$scope.title = "Oops... something went wrong";
$scope.working = false;
});
});
这是我的查看页面:
@{
ViewBag.Title = "Contact";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>
<div ng-app="myApp" ng-controller="questCtrl">
<table>
<tr ng-repeat="option in options">
<td>{{ option.id }}</td>
<td>{{ option.title }}</td>
<td>{{ option.questionId }}</td>
</tr>
</table>
</div>
<address>
One Microsoft Way<br />
Redmond, WA 98052-6399<br />
<abbr title="Phone">P:</abbr>
425.555.0100
</address>
<address>
<strong>Support:</strong> <a href="mailto:Support@example.com">Support@example.com</a><br />
<strong>Marketing:</strong> <a href="mailto:Marketing@example.com">Marketing@example.com</a>
</address>
@section scripts {
@Scripts.Render("~/Scripts/angular.js")
@Scripts.Render("~/Scripts/app/quest-controller.js")
}
我正在使用 ASP.NET 并尝试学习如何使用 Angular 和 .NET 构建一个安静的应用程序。我正在使用本教程angular with ASP.NET 和 ASP.NET 上的 Lynda.com 课程。
这是我的 JSON 响应。我如何获得方括号?我不知道他们为什么在那里。
[
{
"options":[
{"id":1,"title":"2000","questionId":1},
{"id":2,"title":"2001","questionId":1},
{"id":3,"title":"2002","questionId":1},
{"id":4,"title":"2003","questionId":1}],
"id":1,
"title":"When was .NET first released?"
},
{
"options":[
{"id":5,"title":"Contoso Ltd.","questionId":2},
{"id":175,"title":"System.DateTime","questionId":44},
{"id":176,"title":"System.Float","questionId":44}
],
"id":44,
"title":"Which of the following is NOT a value type in the .NET Framework Common Type System?"
}
]
这是一个成功的 JSON 响应的样子:
{
"options":[
{"id":85,"title":"DOS","questionId":22},
{"id":86,"title":"Altair Basic","questionId":22},
{"id":87,"title":"PC Basic","questionId":22},
{"id":88,"title":"Windows","questionId":22}
],
"id":22,
"title":"What was Microsoft's first product?"
}
也许这会有所帮助:
namespace GeekQuiz.Models
{
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class TriviaQuestion
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
public virtual List<TriviaOption> Options { get; set; }
}
}
这段代码是 TriviaQuestion 模型,也许我没有正确对待它。 如果有任何出色的 .NET 程序员正在寻找门徒,请告诉我。
【问题讨论】:
-
您的代码中有错字,您定义了
$scope.options,但使用option.,将其更改为options.,如options.id等... -
Tushar,option.id 中的单个选项是每个循环的内置角度,因此它只是循环的局部变量,我声明为对象名称,然后返回属性那个变量。
-
是的,错过了 ng-repeat 抱歉,你能告诉我控制台日志吗,AFAIK,响应返回 data ,对你来说它是 data.data.options .. 但首先要得出一些结论,我需要 c 你的控制台日志
标签: javascript c# asp.net json angularjs