Spring.net+nhibenate+asp.net MVC +extjs Series 6----asp.net Mvc+extjs

Source: Internet
Author: User
Tags json

Now MVC uses Modelbinder, controller can now accept many complex types of parameters, but for Jquery,extjs and other JS frameworks, more of the JSON format and server-side transfer parameters more reasonable. For simple parameters, We can post directly to the server side without the JSON format. For example, the user login, we only pass the username and password into 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: "Landing Failed",
Msg:response.responseText
});
}
});
} else {
Form.markinvalid ();
Ext.MessageBox.alert ("message", "input error");
}
}

As you can see, the values for UserID and password are not Ext.util.JSON. Encode encrypted into a JSON format string. This corresponds to the login in our last Usercontroller. But for the transport entity, That is, adding users and updating the user's actions is not easy:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult AddUser(UserDTO User){.....}

In a way, we can transmit User.username,user.userid this prefix + attribute as a key in the way of post to controller, MVC through Defaultmodelbinder can use the value

Mapped to an entity parameter, but this way requires that we modify the name of the form's control. Another way is to implement Imodelbinder, add Jsonmodelbinder, and make the foreground JS frame pass the JSON object

To controller to the parameters, let's just do the following simple implementation:

public class JsonBinder<T> : IModelBinder
  {
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
      var param = new DataContractJsonSerializer(typeof(T))
        .ReadObject(controllerContext.HttpContext.Request.InputStream);
      return param;
    }
  }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.