我自動生成的ADO.NET實體數據庫模型爲我angularJS應用程序中使用的數據庫波紋管顯示:ASP.NET - 控制器無法從數據庫中檢索數據
我有從顯示所有結果的問題數據庫。當我調用函數來獲取數據庫中的所有數據時,我得到了這個結果(女巫對我感到困惑):
這是AJAX請求的結果(我不知道如何以及爲什麼結果是HTML頁面/模板)。 :
<!DOCTYPE html>
<html ng-app="contactsManager">
<head>
<title>Contacts</title>
</head>
<body>
<div class="navbar navbar-top">
<link href="/Content/bootstrap.min.css" rel="stylesheet" />
<link href="/Content/custom.css" rel="stylesheet" />
<div class="navbar-inner">
<div class="container">
<h2>Contacts</h2>
</div>
</div>
</div>
<div ng-view class="example-animate-container"
ng-animate="{enter: 'example-enter', leave: 'example-leave'}"></div>
<script src="/Scripts/angular.js"></script>
<script src="/Scripts/angular-route.js"></script>
<script src="/Scripts/application/application.js"></script>
<script src="/Scripts/application/controllers.js"></script>
<script src="/Scripts/application/contactsService.js"></script>
<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
{"appName":"Chrome","requestId":"855308fe8e4849c09afe6746e4d4fab6"}
</script>
<script type="text/javascript" src="http://localhost:53083/9ef1ca62e8c144c68814e88ffffbd4a7/browserLink" async="async"></script>
<!-- End Browser Link -->
</body>
</html>
有人知道我爲什麼得到這個輸出,爲什麼我缺少數據庫中的數據?
下面是代碼,我用:
ContactsController.cs
public class ContactsController : ApiController
{
ContactsEntities db = new ContactsEntities();
//get all
[HttpGet]
public IEnumerable<Contact> Get()
{
return db.Contacts.AsEnumerable();
}
//get customer by id
public Contact Get(int id)
{
Contact contacts = db.Contacts.Find(id);
if (contacts == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return contacts;
}
//update
public HttpResponseMessage Put(int id, Contact contact)
{
if (!ModelState.IsValid)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
if (id != contact.id)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
db.Entry(contact).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
//insert
public HttpResponseMessage Post(Contact contact)
{
if (ModelState.IsValid)
{
db.Contacts.Add(contact);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, contact);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = contact.id }));
return response;
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
這裏是爲應用程序前端angularJS控制器:
app.controller('ContactsController', [
'$scope', '$http', '$location', 'contactsService',
function ($scope, $http, $location, contactsService) {
$http.get('/#/contacts/').success(function (data) {
console.log(data);
$scope.contacts = data;
// $scope.loading = false;
})
.error(function() {
alert ("An Error has occured while loading posts!");
// $scope.loading = false;
});
$scope.editContact = function (id) {
$location.path('/edit-contact/' + id);
};
$scope.displayContact = function (id) {
$location.path('/display-contact/' + id);
};
$scope.showDetails = function (id) {
var el = angular.element(document.getElementById('#ct-details-' + id));
el.toggleClass('details-hidden');
}
}
]);
東西你是從控制器期待JSON數據?如果是這樣,您可能需要將其指定爲返回類型。 – Dan
我跟着教程和導師沒有提到任何關於JSON數據。當我顯示請求數據時,我得到了HTML輸出巫婆有線... – jureispro