从asp.net控制器

时间:2015-06-11 18:06:49

标签: javascript c# asp.net angularjs

我在asp.net中使用angularjs

我使用CRUD制作了一个控制器,并尝试使用$ http service

从angularjs控制器获取数据

路由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; }
}

EDIT3: 如果我命名我的.cs控制器SongsController并导航到url歌曲/索引/某些东西我会弹出如果我想打开或保存something.json并只是重定向回我的默认网址由ngroute(#/ songs /)

但是,如果我将.cs控制器命名为其他东西,比如RandomController,如果我导航到同一个url,我会收到此错误:

  

在序列化类型为#System; Data.Data.Entity.DynamicProxies.Navigation_7A1A3B789B740F23BAB0A6DAABE519BE3A F91C300893047C23FF2FD8C44E6705&#39;的对象时检测到循环引用。

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);*/
    }

如果它&#39;喜欢这一切都有效,但如果看起来我在原帖中写道,当我运行$ http.get时,我得到未定义的错误:/

EDIT5:好的,我相信问题是我试图发送包含Navigation类对象数组的对象,我该如何解决这个问题? :(

1 个答案:

答案 0 :(得分:0)

您的Song课程有一个循环引用。

当Json序列化程序尝试处理它时,它会找到Navigations属性并尝试序列化它,问题是该集合上的每个Navigation对象都有一个相同的实例{ {1}},所以它进入一个无限循环,试图一遍又一遍地序列化所有这些。

这是因为EntityFramework有延迟加载并在序列化程序尝试访问它们时自动填充类。

要修复它,你可以做两件事,特别是禁用该调用的延迟加载:

Song

另一个选项是创建一个模型,其中只包含您需要返回的数据并手动填充(或使用映射工具)。

public ActionResult Index(string id)
{
    db.Configuration.LazyLoadingEnabled = false;

    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);
}