Currently, MVC uses modelbinder. controller can now accept many complex types of parameters. However, for JS frameworks such as jquery and extjs, the JSON format and server-side transmission parameters are more reasonable. for simple parameters, we can directly post them to the server without using the JSON format. for example, if a user logs in, we only pass the user name and password to the Controller:
Handler: Function Checklogin (){ If (Form. isvalid ()){ VaR Formvalue = form. getvalues (); Ext. Ajax. Request ({URL: '/user. MVC/login', method :" Post ", Waitmsg:" Please wait! ", Params: {userid: formvalue. userid, password: formvalue. userpassword}, success: Function (Response, options ){ VaR Responsemessage = ext. util. JSON. Decode (response. responsetext );If (Responsemessage. Result) {win. Destroy (); Window . Location =" /Home. MVC/Index ";} Else {Ext. MessageBox. Alert ("Message", responsemessage. Message) ;}, failure: Function (Response, options) {Ext. messageBox. hide (); Ext. messageBox. show ({Title: "Logon Failed", MSG: response. responsetext });}});} Else {Form. markinvalid (); Ext. MessageBox. Alert ("Message", "input error ");}}
We can see that the userid and password values do not use Ext. util. JSON. encode is encrypted into a JSON string. this corresponds to the login in our last usercontroller. however, it is not so easy to add or update a user to a transmission object:
[Acceptverbs (httpverbs. Post)]PublicActionresult adduser (userdto user ){.....}
In one way, we can transmit the prefix + attribute of user. username, user. userid to the controller using the POST method.
Defaultmodelbinder can map values to object parameters, but in this way, we need to modify the name of the Form Control. Another way is to implement
Imodelbinder: Add jsonmodelbinder so that the front-end JS framework transmits the JSON object to the Controller and parses it to the parameter. Currently, we only perform the following simple implementation:
Public ClassJsonbinder <t>: imodelbinder {Public ObjectBindmodel (controllercontext, modelbindingcontext bindingcontext) {var Param =NewDatacontractjsonserializer (Typeof(T). readobject (controllercontext. httpcontext. Request. inputstream );ReturnParam ;}}
Similarly, we also need to register in application_start () of Global:
Modelbinders. Binders [Typeof(Userdto)] =NewJsonbinder <userdto> ();
In this way, when the front-end JS Framework Requests user. MVC/adduser, data in JSON format can be directly transmitted, and MVC will automatically perform JSON deserialization through jsonbinder,
Parse the userdto object. This implementation is much more powerful than manually converting the parameters in reqeust.
VaR Formvalue = ext. util. JSON. encode (userform. form. getvalues (); Ext. ajax. request ({URL: '/user. MVC/updateuser', method: 'post', Params: formvalue, callback: Function (Options, success, response ){ If (SUCCESS) {Ext. MessageBox. Hide (); VaR Result = ext. util. JSON. Decode (response. responsetext) Ext. MessageBox. Alert ("Message", result. Message );} Else {Ext. MessageBox. Hide (); Ext. MessageBox. Alert ("Failed, please try again", response. responsetext) ;}}, failure: Function (Response, options) {Ext. MessageBox. Hide (); returnvalue = ext. MessageBox. Alert ("Warning", "Exception error! Contact the administrator! ") ;}, Success: Function (Response, options) {Ext. MessageBox. Hide (); store. Reload ();}})
In front-end MVC + extjs integration, some content is omitted, for example, how to use mvccontrib to integrate spring.net and MVC, and use it on the web layer
Log4net records the error information and so on. The content can be directly recorded through the demoProgramIf any problem is found, please contact me directly. Finally, we will show the labor results of the demo program,
Beautiful extjs interface and demo functions.
Download Code.
Database Table creation script.
Author: lone knight (like a year of water)
Source: http://lonely7345.cnblogs.com
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.