ExtJS: how to return json success w/ data using asp.net mvc

前端 未结 3 1522
南旧
南旧 2020-12-29 14:57

I am trying to use ExtJS with Asp.Net MVC, and it is going fine so far. (Nice work on ExtJS) To make things easier, I need some help returning data from .net to ExtJS.

3条回答
  •  孤城傲影
    2020-12-29 15:37

    I used @Wellington's answer with VS2010 (beta2) and MVC 2 (beta), and got the following error:

    Method 'System.String ToString(System.String)' has no supported translation to SQL.

    Which, I think, is a serialization problem (?)

    Here's what I changed to make it work..

    public JsonResult Index()
    {
        var json = new
        {
            success = true,
            data = from user in repository.Users
                   select new JsonUser(user)
        };
        return Json(json);
    }
    

    JsonUser is a simple, serializable object - I got the idea from a podcast by @Scott Hanselman

    Here's an example of JsonUser:

    public class JsonUser
    {
        public long id { get; set; }
        public string name { get; set; }
        public string dateJoined { get; set; }
        ...
    
        public JsonUser(User user)
        {
            id = user.ID;
            name = user.Name;
            dateJoined = user.DateJoined.ToString("yyyy-MM-dd");
            ...
        }
    }
    

提交回复
热议问题