【问题标题】:ExtJS: how to return json success w/ data using asp.net mvcExtJS:如何使用 asp.net mvc 返回带有数据的 json 成功
【发布时间】:2009-09-26 19:51:03
【问题描述】:

我正在尝试将 ExtJS 与 Asp.Net MVC 一起使用,到目前为止一切正常。 (ExtJS 的出色工作) 为了让事情变得更简单,我需要一些帮助,将数据从 .net 返回到 ExtJS。

ExtJS 期望在 JSON 响应中看到成功标志以及其他数据。

一个示例预期响应格式类似于

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

那么,使用 linq2sql 或 ado.net 数据集作为模型对象,你们知道如何轻松以这种格式返回数据吗?

类似

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

顺便说一句,这行得通吗?如果我有一个具有布尔成功和对象数据属性的 ExtJSResult 类?

提前致谢

【问题讨论】:

    标签: c# asp.net-mvc json extjs


    【解决方案1】:

    试试这个...

    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。所以我想出了一个不同的解决方案,它应该适用于好的旧数据集。我想有一天我必须学习并开始使用 Linq。 (到目前为止,我还不能同意使用什么 O/RM,所以我使用了一点 Linq2Sql 和一些经典的 Ado.Net。
    【解决方案2】:

    我正在使用 Newtonsoft.Json 以及 Rick Strahl 的一些代码,它有助于序列化 Data 对象。 他的原帖在这里: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
        };
    
    }
    

    我希望这对我以外的人有用。

    【讨论】:

      【解决方案3】:

      我将@Wellingtonanswer与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 Hanselmanpodcast 得到这个想法

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-28
        • 2014-03-30
        • 1970-01-01
        • 1970-01-01
        • 2016-01-25
        相关资源
        最近更新 更多