我想使用角度js獲取項目列表。我使用角碼並使用$ http。但它不起作用。我點擊操作方法並嘗試返回json。但是當我嘗試它。它返回完整的JSON視圖。angularjs http在使用獲取數據列表時不能在mvc中工作
@section scripts{
<script src="~/Scripts/ang.js"></script>
}
<div ng-app="StudentApp" class="container">
<br/>
<br/>
<input type="text" placeholder="Search Student" ng-model="searchStudent" />
<br />
<div ng-controller="StudentController">
<table class="table" ng-init="initData()">
<tr ng-repeat="r in data | filter : searchStudent" >
<td>{{ r.Col_Title }}</td>
<td>{{ r.Col_Descrp }}</td>
</tr>
<table>
</div>
</div>
public ActionResult GetCollections()
{
var lst = eShoppingEntitiesContetxt.Collections.Where(s => s.IsActive == true).ToList();
var result = JsonConvert.SerializeObject(lst, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
return Json(result, JsonRequestBehavior.AllowGet);
// return View();
}
var myApp = angular.module('StudentApp', []);
myApp.controller('StudentController', function ($scope, $http) {
$http.get('/User/GetCollections') // added an '/' along with deleting Controller portion
.then(function (response) {
$scope.data = response.data
})})
<div ng-app="StudentApp" class="container">
<br/>
<br/>
<input type="text" placeholder="Search Student" ng-model="searchStudent" />
<br />
<div ng-controller="StudentController">
<table class="table">
<tr ng-repeat="r in data | filter : searchStudent" >
<td>{{ r.name }}</td>
<td>{{ r.country }}</td>
</tr>
<table>
</div>
</div>
能告訴你這是返回的JSON? – Kamo
向我們顯示您的控制器,控制器應該返回'JsonResult'而不是'ActionResult'。 –
查看我的控制器方法 –