【问题标题】:How to convert ObjectResult to JsonResult?如何将 ObjectResult 转换为 JsonResult?
【发布时间】:2018-09-09 14:27:25
【问题描述】:
public JsonResult Get()
{
    DBEntities db = new DBEntities();
    var result = db.GetMenuMaster();
    //Convert result to Json
}

我们需要将我们从数据库中得到的结果集转换成json,这样才能在javascript和Angularjs中使用。

【问题讨论】:

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


    【解决方案1】:
    public JsonResult Get()
    {
      DBEntities db = new DBEntities();
      var result = db.GetMenuMaster();
      return new JsonResult() { Data = result, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }
    

    【讨论】:

      【解决方案2】:

      使用也可以试试这个:-

        return Json(new { result }, JsonRequestBehavior.AllowGet);
      

      【讨论】:

        【解决方案3】:

        优化:

        [HttpGet]
            public ActionResult Get()
            {
                DBEntities db = new DBEntities();
                var result = db.GetMenuMaster();
        
                return new JsonResult
                {
                    Data = result,
                    ContentEncoding = Encoding.UTF8,
                    ContentType = "application/json",
                    MaxJsonLength  = int.MaxValue,
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet
                };
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-05
          • 2016-12-11
          • 2018-03-31
          • 1970-01-01
          • 1970-01-01
          • 2014-05-06
          相关资源
          最近更新 更多