从 JSON 响应创建一个可观察的敲除数组

Creating an observable knockout array from a JSON response

C#/MVC/Knockout/JSON

我有以下 javascript:

function Feed(data) {
    this.ID = ko.observable(data.ID);
    this.RSSName = ko.observable(data.RSSName);
    alert(data.RSSName + " " + data.ID);
}

function ViewModel() {
    self = this;
    self.CurrentFeeds = ko.observableArray([]);
    self.isLoading = ko.observable(false);
    self.StatusMessage = ko.observable("Loading");

    $.ajax({
        type: "GET",
        url: '@Url.Action("RSSList", "RSS")',
        success: function (data) {
            var feeds = $.map(data, function (item) {
                alert(item.RSSName + " " + item.ID + " 1");
                return new Feed(item)
            });
            self.CurrentFeeds(feeds);
            //self.CurrentFeeds(data);

        },
        error: function (err) {
            alert(err.status + " : " + err.statusText);
        }
    });

    self.save = function () {
        self.deleteFeed = function (feed) {
        };
    };
}

JSON 响应(从 fiddler 复制)如下所示:

{"aaData":[{"ID":"0","RSSName":"Most Recent"},{"ID":"1","RSSName":"Website feed"}]}

控制器:

    public JsonResult RSSList()
    {
        var query = (from t in db.tblRSSFeeds
                     select new ViewModels.RSSList()
                     {
                         ID = t.pkID.ToString(),
                         RSSName = t.szFeedName
                     }).OrderBy( t => t.RSSName).ToList();

        var recent = new ViewModels.RSSList();
        recent.ID = "0";
        recent.RSSName = "Most Recent";
        query.Insert(0, recent);

        return Json(  query, JsonRequestBehavior.AllowGet);
    }

我认为我的问题与 Feed(data) 函数有关,因为它只传回一条记录。我也尝试设置 self.CurrentFeeds(data) 但没有成功。上面显示的 "alerts" 显示未定义,但我可以看到来自 fiddler 的数据...

出于某种原因,成功函数无法正确查看数据以创建数组。这是为什么?

如果是响应:

{"aaData":[{"ID":"0","RSSName":"Most Recent"},{"ID":"1","RSSName":"Website feed"}]}

success 回调更改为:

$.ajax({
   type: "GET",
   url: '@Url.Action("RSSList", "RSS")',
   success: function (data) {
           var feeds = $.map(data.aaData, function (item) {
           alert(item.RSSName + " " + item.ID + " 1");
           return new Feed(item)
       });
       self.CurrentFeeds(feeds);
   },
   error: function (err) {
       alert(err.status + " : " + err.statusText);
   }
});

而且我相信它有效,因为您试图映射一个对象而不是数组,所以您必须获取要映射的数组 aaData