国外某牛人的JsonModelBinder 实现 MVC 3.0
首先,实现一个json model binder ,判断所有的数据,如果是包含指定contentType的json 则进行 转换
public class JsonModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (!IsJSONRequest(controllerContext))
{
return base.BindModel(controllerContext, bindingContext);
}
// Get the JSON data that's been posted
var request = controllerContext.HttpContext.Request;
var jsonStringData = new StreamReader(request.InputStream).ReadToEnd();
return new JavaScriptSerializer().Deserialize(jsonStringData, bindingContext.ModelMetadata.ModelType);
}
private static bool IsJSONRequest(ControllerContext controllerContext)
{
var contentType = controllerContext.HttpContext.Request.ContentType;
return contentType.Contains("FTchinaMVC/json");
}
}
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (!IsJSONRequest(controllerContext))
{
return base.BindModel(controllerContext, bindingContext);
}
// Get the JSON data that's been posted
var request = controllerContext.HttpContext.Request;
var jsonStringData = new StreamReader(request.InputStream).ReadToEnd();
return new JavaScriptSerializer().Deserialize(jsonStringData, bindingContext.ModelMetadata.ModelType);
}
private static bool IsJSONRequest(ControllerContext controllerContext)
{
var contentType = controllerContext.HttpContext.Request.ContentType;
return contentType.Contains("FTchinaMVC/json");
}
}
然后 全局注册
protected void Application_Start()
{
ModelBinders.Binders.DefaultBinder = new JsonModelBinder();
RegisterRoutes(RouteTable.Routes);
}
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
{
ModelBinders.Binders.DefaultBinder = new JsonModelBinder();
RegisterRoutes(RouteTable.Routes);
}
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
最后是ajax 客户端的一种实现,以Extjs为例子
Ext.Ajax.defaultPostHeader = 'FTchinaMVC/json';
Ext.Ajax.request({
success: function (response, opts) {
if (response.responseText) {
winFormAddMaster.hide();
store.load();
Ext.Msg.alert('系统提示', "成功");
} else {
Ext.Msg.alert("系统提示", '失败');
}
},
failure: function () {
Ext.Msg.alert('系统提示', 'Failure');
},
params: Ext.encode(FormAddMaster.form.getValues()) //以json的形式发送到action
});
Ext.Ajax.request({
url: '/master/Create',
method: "post",success: function (response, opts) {
if (response.responseText) {
winFormAddMaster.hide();
store.load();
Ext.Msg.alert('系统提示', "成功");
} else {
Ext.Msg.alert("系统提示", '失败');
}
},
failure: function () {
Ext.Msg.alert('系统提示', 'Failure');
},
params: Ext.encode(FormAddMaster.form.getValues()) //以json的形式发送到action
});
对应Action,可以做判断是否来自Ajax提交,返回给Ajax更多有用的信息。
[HttpPost]
public ActionResult Create(Master master)
{
if (ModelState.IsValid)
{
db.Master.Add(master);
db.SaveChanges();
}
return View(master);
}
public ActionResult Create(Master master)
{
if (ModelState.IsValid)
{
db.Master.Add(master);
db.SaveChanges();
if (!Request.IsAjaxRequest())
{
return RedirectToAction("Index");
}
else
{
Response.Write("来自ajax提交");
Response.End();
return null;
}
{
return RedirectToAction("Index");
}
else
{
Response.Write("来自ajax提交");
Response.End();
return null;
}
}
return View(master);
}
语言en>zh YahooCE
没有做改动。但是