ajax向后台传入json需要设置option,如下
contentType:'application/json' data:Json.Stringify(jsObj)
后台处理复杂json对象(不知道如果不加这个特性能到widgets? 有待验证)
[JsonFilter(Param = "widgets", JsonDataType = typeof(List< PageDesignWidget> ))] public JsonResult SaveOrUpdate(List< PageDesignWidget> widgets { public class JsonFilter : ActionFilterAttribute { public string Param { get; set; } public Type JsonDataType { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.Request.ContentType.Contains("application/json")) { string inputContent; using (var sr = new StreamReader(filterContext.HttpContext.Request.InputStream)) { inputContent = sr.ReadToEnd(); } var result = JsonConvert.DeserializeObject(inputContent, JsonDataType); filterContext.ActionParameters[Param] = result; } } }
相关链接
Jquery Ajax方法传递json到action
如何post复杂对象的数组, 在JSON , jQuery ASP.Net MVC Controller ?