在MVC2中使用Ajax提交参数为对象转换
众所周知,在asp.net mvc3中支持前端提交的JSON数据,但是在mvc2中并不支持,但是可以通过实现IModelBinder接口,变相的实现接收json的数据,
首先,我还是先贴出相关的代码在说
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MvcApplication1.Models; using System.Web.Script.Serialization; namespace MvcApplication1.Binders { public class UserParsBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { string f= controllerContext.HttpContext.Request["user"]; JavaScriptSerializer js = new JavaScriptSerializer(); UserT u= js.Deserialize(f,typeof(UserT)) as UserT; //UserT u = js.Deserialize<UserT>(f); return u; } } }
上面的代码就是关于实现IModelBinder接口的代码其中UserT类就是对应Model,其中先是通过Form表得到前端传送的Json数据,然后通过,JavaScriptSerializer 进行对应序列化,转成相关的实体类或者是对应集合,最后返回所需要的结果,
至于IModelBinder的用途是什么呢。能够让我们通过代码想Action里面传递各种类型的参数,从而达到某个需要
其中BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
从controllerContext属性--》控制器上下文。bindingContext--》绑定模型的上下文,上述的操作仅仅是小部分,在使用UserParsBinder这个类的,可以把它设置为全局性的,或者是单个Action参数里面,
下面,就是把UserParsBinder设置为全局性,那么此时我们将在Global文件中,添加相关的代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 protected void Application_Start() 2 { 3 AreaRegistration.RegisterAllAreas(); 4 //单个对象 5 // ModelBinders.Binders[typeof(UserT)] = new UserParsBinder(); 6 //泛型集合 7 ModelBinders.Binders[typeof(IList<UserT>)] = new ListUserParsBinder(); 8 RegisterRoutes(RouteTable.Routes); 9 }
页面中得到的Json数据
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Login</title> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script src="Scripts/json2.js" type="text/javascript"></script> <script type="text/javascript"> function UserT() { this.uid = $("#txtUid").val(); this.pwd = $("#txtPwd").val(); } UserT.prototype = { constructor: UserT } $(document).ready(function () { $("#btnTest").click(function () { $.post("Test/Login", { user: JSON.stringify(new UserT()) }, function (result, resultState) { if (resultState == "success") { alert(result); } }); }); $("#btnTT").click(function () { var arr = new Array(); for (var i = 0; i < 10; i++) { arr.push(new UserT()); } $.post("Test/userList", { user: JSON.stringify(arr) }, function (result, resultState) { if (resultState == "success") { alert(result); } }); }); }); </script> </head> <body> <div> <input type="text" value="测试数据Uid" id="txtUid" /><br /> <input type="text" value="测试数据pwd" id="txtPwd" /><br /> <input type="button" value="单个对象Json提交" id="btnTest" /><br/> <input type="button" value="集合Json提交" id="btnTT" /> </div> </body> </html>
在对应Json转换成数据的时候,我主要是从Json2,这个Jquery插件来做,在《360极速浏览器》中,可以不用引用这个插件,直接使用JSON.stringify(arr)函数即可。
先简单介绍,有空在详解。。。。