asp.net mvc - C# How To return List<> As Json -


i having problem json , lists

i trying return list of chat entity class when try return compiler whines. tried return ienumerable<> same error came

what can ?

here function returning item,

public list<chat> getnewchatposts() {      var userid = u_rep.getuserid(user.identity.name);     var list = g_rep.getnewestchat(0, userid);     return json(list); } 

this newest chat function

public list<chat> getnewestchat(int gameid, int userid) {      var pos1 = (from p in n_db.chatpos                 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 = chat in n_db.chats                             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;     } 

and ajax call

$.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"); } }); 

the compiler unhappy code because saying method (i'm guessing action method) defined return list<chat> , returning jsonresult. if using asp.net mvc should this:

public actionresult getnewchatposts() {      var userid = u_rep.getuserid(user.identity.name);     var list = g_rep.getnewestchat(0, userid);      return json(list); } 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -