ExtJS:如何使用asp.net mvc返回带有数据的json成功。

7
我正在尝试使用ExtJS和Asp.Net MVC,目前进展顺利(ExtJS做得很好)。为了使事情变得更容易,我需要一些帮助从.net向ExtJS返回数据。
ExtJS期望在JSON响应中看到一个成功标志以及其他数据。
样例预期响应格式如下:
{success: true, data: {id: 3, text: "hello world}}
因此,是否有使用linq2sql或ado.net dataset作为模型对象时,轻松地返回此格式数据的想法?
类似于以下内容:
public JsonResult Index()
{
  result.success= true;
  result.obj = repository.FindAllUsers();
  return Json(result)
}

顺便问一下,如果我有一个名为ExtJSResult的类,其中包含bool success和Object data属性,这样行吗?

提前感谢。

3个回答

13

试一下这个...

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);
}

看起来非常不错。Linq让我感到非常害怕,所以在这种情况下我一直在使用DataSets。因此,我想出了一个不同的解决方案,应该可以使用老旧的DataSets工作。我猜总有一天我会学习并开始使用Linq。(到目前为止,我只是无法确定要使用哪个O/RM,所以我正在使用一些Linq2Sql和一些经典的Ado.Net。) - hazimdikenli

0
我正在使用Newtonsoft.Json和Rick Strahl的一些代码,这有助于序列化数据对象。 他的原始帖子在这里:http://www.west-wind.com/Weblog/posts/471835.aspx
    public class ExtJSJsonResult : JsonResult
    {
        public bool success { get; set; }
        public string msg { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null){
                throw new ArgumentNullException("context");}

            HttpResponseBase response = context.HttpContext.Response;

            if (!String.IsNullOrEmpty(ContentType))
            {
                response.ContentType = ContentType;
            }
            else
            {
                response.ContentType = "application/json";
            }
            if (ContentEncoding != null)
            {
                response.ContentEncoding = ContentEncoding;
            }
            if (Data != null)
            {
                Type type = Data.GetType();
                response.Write(String.Format("{{success: true, msg: \"{0}\", data:", msg));
                if (type == typeof(DataRow))
                    response.Write(JSonHelper.Serialize(Data, true));
                else if (type == typeof(DataTable))
                    response.Write(JSonHelper.Serialize(Data, true));
                else if (type == typeof(DataSet))
                    response.Write(JSonHelper.Serialize(Data, true));
                else
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    response.Write(serializer.Serialize(Data));
                }
                response.Write("}");
            }
        }
    }

使用它

public ExtJSJsonResult View(int id)
{
    bool success;
    string msg;
    DataRow dr=null;
    try
    {
        dr = DBService.GetRowById("oc.personeller", id);
        success = true;
        msg = "all ok";
    }
    catch (Exception ex)
    {
        success = false;
        msg = ex.Message;
    }

    return new ExtJSJsonResult
    {
        success= success,
        msg = msg,
        Data = dr
    };

}

我希望这对除了我以外的其他人有所帮助。


0

我使用 @Wellington答案 与 VS2010 (beta2) 和 MVC 2 (beta),并得到以下错误:

方法“System.String ToString(System.String)”不支持转换为 SQL。

我认为这是一个序列化问题 (?)

这是我所做的更改使其工作..

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

JsonUser 是一个简单的可序列化对象 - 我从 @Scott Hanselman播客 中得到了这个想法。

这里是一个 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");
        ...
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接