【发布时间】:2015-06-11 18:06:48
【问题描述】:
我在 asp.net 中使用 angularjs
我使用 CRUD 制作了一个控制器,并尝试使用 $http 服务从 angularjs 控制器获取数据
Route params 正在从 url 获取正确的查询,我对此进行了测试,但在请求数据时出现未定义的错误
我做错了什么? :(
SongsController.cs 方法:
public ActionResult Index(string id)
{
/*var result = db.Songs.ToList();
return Json(result, JsonRequestBehavior.AllowGet);*/
string searchString = id;
var songs = from m in db.Songs
select m;
if (!String.IsNullOrEmpty(searchString))
{
songs = songs.Where(s => s.Title.Contains(searchString));
}
return Json(songs, JsonRequestBehavior.AllowGet);
}
songsService.js:
myApp.factory('songsService', ['$http', function ($http) {
var songsService = {};
songsService.getSongs = function (param) {
return $http.get('/Songs/Index/' + param);
}
return songsService;}])
songsController.js:
myApp.controller('songsController', ['$scope', '$routeParams', 'songsService', function ($scope, $routeParams, songsService) {
var search = $routeParams.query;
if (search == 'undefined' || search == null)
search = '';
getSongs(search);
function getSongs(searchText) {
songsService.getSongs(searchText)
.success(function (data) {
$scope.songs = data;
})
.error(function (error) {
$scope.status = 'Unable to load data: ' + error.message;
console.log($scope.status);
});
}}]);
编辑: 歌曲类:
using System;
using System.Collections.Generic;
public class Song
{
public int ID { get; set; }
public string Title { get; set; }
public string Artist { get; set; }
public virtual ICollection<Navigation> Navigations { get; set; }
}
EDIT2:导航类:
using System;
public class Navigation
{
public int ID { get; set; }
public int SongID { get; set; }
public int PlaylistID { get; set; }
public virtual Song Song { get; set; }
public virtual Playlist Playlist { get; set; }
}
编辑3: 如果我命名我的 .cs 控制器 SongsController 并导航到 url song/index/something 如果我想打开或保存 something.json 并被重定向回由 ngroute (#/songs/) 定义的默认 url,我会弹出窗口
但是,如果我将 .cs 控制器命名为其他名称,例如 RandomController,如果我导航到相同的 url,我会收到此错误:
在序列化“System.Data.Entity.DynamicProxies.Navigation_7A1A3B789B740F23BAB0A6DAABE519BE3AF91C300893047C23FF2FD8C44E6705”类型的对象时检测到循环引用。
EDIT4:如果我的 SongsController.cs 看起来像这样,我已经指出一切:
public ActionResult Index(string id)
{
var song = new List<Song>
{
new Song{Title="Paint It Black",Artist="Rolling Stones"},
new Song{Title="People Are Strange",Artist="The Doors"},
new Song{Title="With Or Without You",Artist="U2"},
new Song{Title="Wish You Were Here",Artist="Pink Floyd"},
new Song{Title="Fluorescent Adolescent",Artist="Arctic Monkeys"},
new Song{Title="La Guitarra",Artist="Orjan Nilsen"},
new Song{Title="Ping Pong",Artist="Armin Van Buuren"},
new Song{Title="Fade Out Lines",Artist="The Avenger"},
new Song{Title="Redemption Song",Artist="Bob Marley"},
new Song{Title="Wherever I May Roam",Artist="Metallica"},
};
return Json(songs, JsonRequestBehavior.AllowGet);*/
}
如果一切正常,但如果它看起来像我在原始帖子中写的那样,当我运行 $http.get 时出现未定义错误:/
EDIT5:好的,我相信问题是我正在尝试发送包含导航类对象数组的对象,我该如何解决这个问题? :(
【问题讨论】:
-
您能分享一下您的
Songs课程吗?你检查过循环引用吗?此外,最好不要自己返回实体对象,尝试为返回值制作投影和/或模型类。 -
我添加了 Song 类,Songs 是上下文类 PlayerContext 的一部分
-
您是否尝试通过将 url 复制并粘贴到浏览器中来执行独立于 Angular 的 get 请求?这有什么回报吗?如果不是,问题可能是您在 MVC 中没有正确设置路由:msdn.microsoft.com/en-us/library/cc668201.aspx
-
如果这样做,我会收到此错误:在序列化类型为“System.Data.Entity.DynamicProxies.Navigation_7A1A3B789B740F23BAB0A6DAABE519BE3AF91C300893047C23FF2FD8C44E6705”的对象时检测到循环引用。
-
添加了导航类 PLUS
标签: javascript c# asp.net angularjs