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;
}
}