提供一个Json传到服务端序列化json参数的Filter类供大家参考
2010-05-04 21:08
253 查看
在网上找了半天关于如何从前端json传过来的数据,到后台获取。
主要思路如下,通过JsonParamFilter类来序列化从前端获取的数据。
JsonParamFilter.cs:
代码
Action Method里的使用方法:
[JsonParamFilter(TargetType = typeof(EmployeeInfo), Param = "employeeInfo")]
public ActionResult TestJson(EmployeeInfo employeeInfo)
{
return Json(employeeInfo);
}
客户端调用:
<script type="text/javascript">
var employee = new Object();
employee.Name = "人员1";
employee.Age = 25;
employee.Salary = 12345;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Home/TestJson/",
data: $.toJSON(employee), // 序列化JSON对象,用了一个叫 jquery-json 的插件
dataType: "json",
success: function(json) {
alert("Name:" + json.Name + ", Age:" + json.Age + ", Salary:" + json.Salary);
}
});
</script>
jquery-json 的插件 jquery.json-2.2.js 大家到网上搜一下就有,在这里就不列出来了。
主要思路如下,通过JsonParamFilter类来序列化从前端获取的数据。
JsonParamFilter.cs:
代码
using System; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization; // 需要引用 System.Runtime.Serialization using System.Runtime.Serialization.Json; // 需要引用 System.ServiceModel.Web using System.Web; using System.Web.Mvc; ///<summary> ///使Action Method可以接收序列化后的JSON对象并转换为强类型参数 ///</summary> public class JsonParamFilter : ActionFilterAttribute { ///<summary> ///类型名称 ///</summary> public Type TargetType { get; set; } ///<summary> ///类型对应的参数名称 ///</summary> public string Param { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { if ((filterContext.HttpContext.Request.ContentType ?? string.Empty).Contains("application/json")) { try { object o = new DataContractJsonSerializer(TargetType).ReadObject(filterContext.HttpContext.Request.InputStream); filterContext.ActionParameters[Param] = o; } catch { } } } }
Action Method里的使用方法:
[JsonParamFilter(TargetType = typeof(EmployeeInfo), Param = "employeeInfo")]
public ActionResult TestJson(EmployeeInfo employeeInfo)
{
return Json(employeeInfo);
}
客户端调用:
<script type="text/javascript">
var employee = new Object();
employee.Name = "人员1";
employee.Age = 25;
employee.Salary = 12345;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/Home/TestJson/",
data: $.toJSON(employee), // 序列化JSON对象,用了一个叫 jquery-json 的插件
dataType: "json",
success: function(json) {
alert("Name:" + json.Name + ", Age:" + json.Age + ", Salary:" + json.Salary);
}
});
</script>
jquery-json 的插件 jquery.json-2.2.js 大家到网上搜一下就有,在这里就不列出来了。
相关文章推荐
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目
- dbus参考代码--在一个dbus方法中调用另外一个dbus服务端提供的方法
- 提供一个逻辑问题的算法!(最近打的大家参考一下)
- 关于二级指针的使用测试小例子,仅供测试--参数传递的时候用指针最好,因为压栈的时候指针,只是压入一个地址的值,最多4个字节(32位机上),提供的3个测试例子,基于VC6.0的环境。
- 序列化上面创建的Person对象,使其成为一个JSON字符串
- hibernate 为什么持久化类时必须提供一个不带参数的默认构造函数
- ######保存角色,add表单页面Ajax传参给action,多个集合属性,【较为复杂的页面传参(封装ajax json参数)】:注意表单数据序列化,jquery遍历,json对象转String
- Object序列化成一个字符串(JSON的序列化)
- Dahua Lin是香港中文大学汤晓鸥教授的高徒,在计算机视觉/机器学习方面有很深的造诣。他在自己的主页上有一个推荐书表,值得大家作为参考。 全英文版的,感觉到与国际接轨的压力了!!!
- android开发环境的搭建,基于eclipse.最后会附上一些参考网址算是一个合集给大家
- JMeter之Json Path Extractor使用(解决json接受上一个请求参数)
- 发现一篇不错的博文,和大家分享一下,为有志成为一名优秀前端工程师的童鞋们提供一个
- 给大家做了一个关于日程的例子,可以参考一下,对进一步学习有所帮助
- weixin4j开发—为大家提供一个获取Weixin对象的工具类
- 我自己创建一个一个 JsonHelper 类,只是供大家参考
- 解决Apache启动问题—(OS 10022)提供了一个无效的参数
- 提供一个PHP通用分页Function给大家
- 刚做的一个网页计数器 大家参考一下
- jquery 序列化整个表单作为参数传到后台
- jquery 序列化整个表单作为参数传到后台