zoukankan      html  css  js  c++  java
  • asp.net mvc4 Json问题

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Script.Serialization;
    
    namespace MvcAppDemo.Controllers
    {
        public class UserController : Controller
        {
            private DBEntities db = new DBEntities();
            /// <summary>
            /// 使用mvc Json序列化对象
            /// </summary>
            /// <returns>返回是Json数据    数据类型:Json</returns>
            public ActionResult Index()
            {
                var list = db.UserInfo.ToList();
                //如果action是get请求,要加上 JsonRequestBehavior.AllowGet 这个参数
                return Json(list,JsonRequestBehavior.AllowGet);
            }
    
            // /// <summary>
            /// 使用mvc Json序列化对象
            /// </summary>
            /// <returns>返回是Json数据    数据类型:Json</returns>
            [HttpPost]
            public ActionResult Demo()
            {
                var list = db.Account.ToList();
                //如果action是post请求,可以不需要加上 JsonRequestBehavior.AllowGet 这个参数
                return Json(list);
            }
    
            /// <summary>
            /// 使用mvc Json序列化对象
            /// </summary>
            /// <returns>返回是Json数据    数据类型:Json</returns>
            [HttpPost]
            public ActionResult Demo2()
            {
                var list = db.Account.ToList();
                //如果action是post请求,也可以加上 JsonRequestBehavior.AllowGet 这个参数
                return Json(list,JsonRequestBehavior.AllowGet);
            }
    
    
            /***
             * 
             * 使用JavaScriptSerializer 序列化对象
             * 
             * ***/
            /// <summary>
            /// 使用JavaScriptSerializer 序列化对象
            /// </summary>
            /// <returns>返回是Json数据字符串   数据类型:string</returns>
            public ActionResult JSS()
            {
                var list = db.Account.ToList();
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string content = jss.Serialize(list);
                //返回是json数据的字符串数据
                return Content(content);
            }
    
            public ActionResult JSS2()
            {
                //将 Response.ContentType = "application/json"; 就变成json数据
                Response.ContentType = "application/json";
                var list = db.Account.ToList();
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string content = jss.Serialize(list);
                //返回是json数据的字符串数据
                return Content(content);
            }
    
    
            [HttpPost]
            public ActionResult JSS3()
            {
               
                var list = db.Account.ToList();
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string content = jss.Serialize(list);
                //返回是json数据的字符串数据
                return Content(content);
            }
    
            [HttpPost]
            public ActionResult JSS4()
            {
                //将 Response.ContentType = "application/json"; 就变成json数据
                Response.ContentType = "application/json";
                var list = db.Account.ToList();
                JavaScriptSerializer jss = new JavaScriptSerializer();
                string content = jss.Serialize(list);
                //返回是json数据的字符串数据
                return Content(content);
            }
    
    
    
        }
    }
  • 相关阅读:
    hadoop中namenode发生故障的处理方法
    开启虚拟机所报的错误:VMware Workstation cannot connect to the virtual machine. Make sure you have rights to run the program, access all directories the program uses, and access all directories for temporary fil
    Hbase的安装与部署(集群版)
    分别用反射、编程接口的方式创建DataFrame
    用Mapreduce求共同好友
    SparkSteaming中直连与receiver两种方式的区别
    privot函数使用
    Ajax无刷新显示
    使用ScriptManager服务器控件前后台数据交互
    数据库知识
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/4401509.html
Copyright © 2011-2022 走看看