12

Json とリストに問題があります

Chat Entity クラスのリストを返そうとしていますが、それを返そうとするとコンパイラが泣き言を言います。IEnumerable<> も返そうとしましたが、同じエラーが発生しました

私に何ができる ?

これがアイテムを返す私の関数です、

public List<Chat> GetNewChatPosts()
{ 
    var userID = U_rep.GetUserID(User.Identity.Name);
    var list = G_rep.GetNewestChat(0, userID);
    return Json(list);
}

これはGet Newest Chat機能です

public List<Chat> GetNewestChat(int gameID, int userID)
{ 
    var pos1 = (from p in n_db.ChatPos
                where p.userID == userID && gameID == p.gameID
                select p).SingleOrDefault();
    int pos;
    if (pos1 == null)
    {
        pos = 0;
        ChatPo n = new ChatPo();
        n.gameID = gameID;
        n.userID = userID;
        n.chatID = pos;

        n_db.ChatPos.InsertOnSubmit(n);
        n_db.SubmitChanges();
    }
    else
    {
        pos = pos1.ID;
    }

    var newIEnumerable = from chat in n_db.Chats
                            where chat.ID > pos
                            orderby chat.ID descending
                            select chat;
    List<Chat> newestChat = new List<Chat>();
    foreach (var n in newIEnumerable)
    {
        newestChat.Add(n);
    }

    var last = newIEnumerable.Last();

    pos1.ID = last.ID;

    n_db.SubmitChanges();

    return newestChat;    
}

これはAjax呼び出しです

$.ajax({
    type: "GET",
    url: "/Game/GetNewChatPosts",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify(text),
    success: function (data) {
        alert("success post");
    },
    error: function () { alert("error post"); }
});
4

1 に答える 1

22

あなたのメソッド(私はアクションメソッドを推測しています)が戻るように定義されていてList<Chat>、あなたが戻っていると言っているので、コンパイラはあなたのコードに不満を持っていますJsonResult。ASP.NET MVC を使用している場合は、次のようになります。

public ActionResult GetNewChatPosts()
{ 
    var userID = U_rep.GetUserID(User.Identity.Name);
    var list = G_rep.GetNewestChat(0, userID);

    return Json(list);
}
于 2013-05-10T08:15:57.693 に答える