.NET MVC ToList() 转Json

#region  方法一
        #region ToList()转json
        /// <summary>
        /// 通过类别 Id 获相应产品
        /// </summary>
        /// <returns></returns>
        public JsonResult ProductList(string index)
        {
            //根据id 获取所需要的Models 数据列表 
            
            model m = new model();
            var pList = ProductService.GetEntities(x => x.CategoryId == Convert.ToInt32(index) & x.IsDelete == false).OrderByDescending(x => x.Sort).ToList();
            string sList = string.Empty;
            string jsonstr = string.Empty;
            foreach (var item in pList)
            {
                m.id = item.Id;
                m.img = item.Img;
                m.name = item.Name;
                m.intro = item.intro;
                m.money = item.Money;
                m.keyword = item.Keyword;
                sList += "," + JsonConvert.SerializeObject(m);
            }
            jsonstr += "{\"model\": [" + sList.Substring(1) + "]}";
            return Json(jsonstr);
        }
        #endregion
        #region Model
        public class model
        {
            public int id;
            public string img;
            public string name;
            public string intro;
            public decimal money;
            public string keyword;
        }
        #endregion
        #endregion

 

posted @ 2018-04-10 11:01  紫晶城  阅读(1439)  评论(0编辑  收藏  举报