帮助类:
public class JsonResultPro : JsonResult
{
public JsonResultPro() { }
public JsonResultPro(object data, JsonRequestBehavior behavior)
{
base.Data = data;
base.JsonRequestBehavior = behavior;
this.DateTimeFormat = "yyyy-MM-dd hh:mm:ss";
}
public JsonResultPro(object data, String dateTimeFormat)
{
base.Data = data;
base.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
this.DateTimeFormat = dateTimeFormat;
}
/// <summary>
/// 日期格式
/// </summary>
public string DateTimeFormat { get; set; }
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if ((this.JsonRequestBehavior == JsonRequestBehavior.DenyGet) && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("MvcResources.JsonRequest_GetNotAllowed");
}
HttpResponseBase base2 = context.HttpContext.Response;
if (!string.IsNullOrEmpty(this.ContentType))
{
base2.ContentType = this.ContentType;
}
else
{
base2.ContentType = "application/json";
}
if (this.ContentEncoding != null)
{
base2.ContentEncoding = this.ContentEncoding;
}
if (this.Data != null)
{
//转换System.DateTime的日期格式到 ISO 8601日期格式
//ISO 8601 (如2008-04-12T12:53Z)
IsoDateTimeConverter isoDateTimeConverter = new IsoDateTimeConverter();
//设置日期格式
isoDateTimeConverter.DateTimeFormat = DateTimeFormat;
//序列化
String jsonResult = JsonConvert.SerializeObject(this.Data, isoDateTimeConverter);
//相应结果
base2.Write(jsonResult);
}
}
}
=====================================
调用示例:
return new JsonResultPro(result_json, "yyyy-MM-dd HH:mm:ss");
如:
=====================================
调用示例:
return new JsonResultPro(result_json, "yyyy-MM-dd HH:mm:ss");
如:
[HttpPost]
public JsonResult DR_AJAX_Preasign(string zjhm,string km)
{
var presigninfo = Jaya.ModelDB.Common.SmileAngel.GetModel<PREASIGN>("*", "1=1");
return new JsonResultPro(presigninfo, "yyyy-MM-dd HH:mm:ss");
}
=====================
支持自定义匿名对象
如:
[HttpPost]
=====================
支持自定义匿名对象
如:
[HttpPost]
public JsonResult DR_AJAX_Preasign(string zjhm,string km)
{
var presigninfo = new { name="张三",date=DateTime.Now,sex="女" };
return new JsonResultPro(presigninfo, "yyyy-MM-dd HH:mm:ss");
}
或者 嵌套对象~
[HttpPost]
或者 嵌套对象~
[HttpPost]
public JsonResult DR_AJAX_Preasign(string zjhm,string km)
{
userInfo user=new userInfo();
user.user_name="李四";
user.user_password="123";
var ss = new {
name="张三",
date=DateTime.Now,
sex="女",
userinfo = user
};
return new JsonResultPro(presigninfo, "yyyy-MM-dd HH:mm:ss");
}
public class userInfo//这是个类 对象
{
public string user_name { set; get; }
public string user_password { set; get; }
}
那么ajax回到前台可以直接访问本对象 如:
$.post('/Drv_import/DR_AJAX_Preasign', { zjhm: zjhm,km:$('#_km').val() }, function (data)
{
var name= data.name;
var date= data.date;
var sex= data.sex
var uname= data.userinfo.user_name
var upwd= data.userinfo.user_password
}
======那么ajax回到前台可以直接访问本对象 如:
$.post('/Drv_import/DR_AJAX_Preasign', { zjhm: zjhm,km:$('#_km').val() }, function (data)
{
var name= data.name;
var date= data.date;
var sex= data.sex
var uname= data.userinfo.user_name
var upwd= data.userinfo.user_password
}