Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

ExtJS expects to see a success flag in the JSON Respone along with additional data.

a sample expectedresponse format is something like

{success: true, data: {id: 3, text: "hello world}}

so, using either linq2sql or ado.net dataset for model objects, do you guys have any idea how to easily return data in this format.

Something like

public JsonResult Index()
{
  result.success= true;
  result.obj = repository.FindAllUsers();
  return Json(result)
}

would that work by the way? if I had a ExtJSResult class with bool success and Object data properties?

thanks in advance

like image 390
hazimdikenli Avatar asked Sep 26 '09 19:09

hazimdikenli


1 Answers

Try this one...

public JsonResult Index()
{
    var json = new
    {
        success = true,
        data = from user in repository.FindAllUsers().AsQueryable()
               select new
               {
                   id = user.Id,
                   name = user.Name,
                   ...
               }
    };
    return Json(json);
}
like image 57
Wellington Avatar answered Nov 11 '22 07:11

Wellington