使用mvc测试Demo

================view================

@{    ViewBag.Title = "Index";    Layout = null;}

ajax请求参数测试

    
        
            
                
                    
                        
                        
                    
                    
                        
                        
                    
                    
                        
                        
                    
                    
                        
                        
                    
                
            
            
    
    
    
    
    Array.prototype.toJson = function () {        var json = {};        for (var i in this) {            var o = this[i];            if (o.name != '__VIEWSTATE') {                if (typeof (json[o.name]) == 'undefined')                    json[o.name] = o.value;                else                    json[o.name] += "," + o.value;            }        }        return json;    }    $('#inpBtn').click(function () {        debugger;        var _formObj = $('#_form');        var arr = _formObj.serializeArray();//获取表单数据        for (var i = 0, l = arr.length; i < l;i++){            if (arr[i].name == "ListStr") {                var list = arr[i].value.split(',');                delete arr[i];                for (var j = 0, len = list.length; j < len; j++) {                    var item = {                        name: "ListStr",                        value: list[j]                    };                    arr.push(item);                }                break;            }        }        $.ajax({            url: '/AjaxRequest/GetTestAjax',            type: 'get',            data: arr,            async: false,            success: function (data, status) {                debugger;            },            error: function (ex) {                debugger;            }        });    });    $('#inpBtn2').click(function () {        debugger;        var _formObj = $('#_form');        var arr = _formObj.serializeArray();        var jsonObj = arr.toJson();        jsonObj.ListStr = jsonObj.ListStr.split(',');        var json = JSON.stringify(jsonObj);//object转字符串        $.ajax({            url: '/AjaxRequest/GetTestAjax2',            type: 'get',            data: { 'json': json },            async: false,            success: function (data) {                debugger;            },            error: function (ex) {                debugger;            }        });    });    $('#inpBtn3').click(function () {        debugger;        var _formObj = $('#_form');        var arr = _formObj.serializeArray();        var jsonObj = arr.toJson();        jsonObj.ListStr = jsonObj.ListStr.split(',');        var aaa = jsonObj.ListStr;        //var list = JSON.stringify(aaa);        //var json = JSON.stringify(jsonObj);//object转字符串        var para = {            test: jsonObj,            list: aaa        };        var paraStr = JSON.stringify(para);        $.ajax({            url: '/AjaxRequest/GetTestAjax3',            type: 'post',            contentType: "application/json",            data: paraStr,            async: false,            success: function (data) {                debugger;            },            error: function (ex) {                debugger;            }        });    });    $('#inpBtn4').click(function () {        debugger;        var _formObj = $('#_form');        var arr = _formObj.serializeArray();        var jsonObj = arr.toJson();        jsonObj.ListStr = jsonObj.ListStr.split(',');        var json = JSON.stringify(jsonObj);//object转字符串        $.ajax({            url: '/AjaxRequest/GetTestAjax4',            type: 'post',            contentType: "application/json",            data: json,            async: false,            success: function (data) {                debugger;            },            error: function (ex) {                debugger;            }        });    });

===============controller===============

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Script.Serialization;namespace ContentReplace.Controllers{    public class AjaxRequestController : Controller    {        //        // GET: /AjaxRequest/        public ActionResult Index()        {            return View();        }        public JsonResult GetTestAjax(TestClass test)        {            var data = "";            return Json(data, JsonRequestBehavior.AllowGet);        }        public JsonResult GetTestAjax2()        {            var json = Request["json"];            var jss = new JavaScriptSerializer();            var arr = jss.Deserialize
(json);            var data = "";            return Json(data, JsonRequestBehavior.AllowGet);        }        [HttpPost]        public JsonResult GetTestAjax3(TestClass test,List
 list)        {            var data = "";            return Json(data, JsonRequestBehavior.AllowGet);        }        [HttpPost]        public JsonResult GetTestAjax4(TestClass test)        {            var data = "";            return Json(data, JsonRequestBehavior.AllowGet);        }    }    public class TestClass    {        public int ID { get; set; }        public string Name { get; set; }        public DateTime Date { get; set; }        public List
 ListStr { get; set; }    } }