Я пытаюсь реализовать AngularJS с ASP.NET MVC, и я застрял в нечетной задаче. Я использовал $http.get()
, чтобы запросить список пользовательского класса Question
, но он попадает в блок ошибок углового контроллера.Служба AngularJS Controller Logs undefined error для пользовательской коллекции классов
Когда я возвращаюсь List<sting>
, он хорошо работает и не регистрирует никаких ошибок и отображается в пользовательском интерфейсе.
Вот мой код:
AngularJS:
var BlogPostApp = angular.module('BlogPostApp', [])
BlogPostApp.controller('BlogPostController', function ($scope, BlogPostService) {
getPosts();
function getPosts() {
BlogPostService.getPosts()
.success(function (posts) {
$scope.posts = posts;
console.log($scope.posts);
})
.error(function (error) {
$scope.status = 'Unable to load blog post data: ' + error.message;
console.log($scope.status);
});
}
});
// Services
BlogPostApp.factory('BlogPostService', ['$http', function ($http) {
var BlogPostService = {};
BlogPostService.getPosts = function() {
return $http.get('/Editor/GetPosts');
};
return BlogPostService;
}]);
MVC код
Когда я пытаюсь вернуть List<Question>
, он записывает неопределенная ошибка
public JsonResult GetPosts()
{
List<Question> posts = PostRepository.Posts(10);
return Json(posts, JsonRequestBehavior.AllowGet);
}
Прекрасно работает с List<string>
public JsonResult GetPosts()
{
List<string> lst = new List<string>();
lst.Add("aa");
lst.Add("bb");
return Json(lst, JsonRequestBehavior.AllowGet);
}
Объект Класс:
public partial class Question
{
PIdbEntities db = null;
public Question()
{
this.QAMaps = new HashSet<QAMap>();
db = new PIdbEntities();
}
public int Id { get;set; }
public string Text { get; set; }
public string Html { get; set; }
public string Code { get; set; }
public string Title { get; set; }
public string ShortDescription { get; set; }
public System.DateTime PostedOn { get; set; }
public Nullable<System.DateTime> ModifiedOn { get; set; }
public string Meta { get; set; }
public string UrlSlug { get; set; }
public bool IsPublished { get; set; }
public int UserId { get; set; }
public virtual ICollection<QAMap> QAMaps { get; set; }
public IList<Answer> Answers { get; set; }
}
если вы вернете список, тогда он хорошо работает? вы имеете в виду вместо списка ? –
LiranBo
@ LiranBo; Правильно, журнал Console показывает '{[aa], [bb]}. но не тогда, когда я возвращаю данные JSON с помощью «Список» –
вы можете опубликовать ур вопрос – LiranBo