解决Asp.net Mvc返回JsonResult中DateTime类型数据格式问(2)

以上是Controller类来实例化JsonResult的所有代码。我们只需写一个BaseController类,重写最后一个方法即可,然后我们自己的Controller在继承BaseController即可

下面是BaseController类的部分代码,我们为方便自己个性化的需要又定义了两个MyJosn的方法

/// <summary> /// 返回JsonResult /// </summary> /// <param>数据</param> /// <param>内容类型</param> /// <param>内容编码</param> /// <param>行为</param> /// <returns>JsonReuslt</returns> protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior) { return new CustomJsonResult { Data = data, ContentType = contentType, ContentEncoding =contentEncoding, JsonRequestBehavior = behavior, FormateStr = "yyyy-MM-dd HH:mm:ss" }; } /// <summary> /// 返回JsonResult.24 /// </summary> /// <param>数据</param> /// <param>行为</param> /// <param>json中dateTime类型的格式</param> /// <returns>Json</returns> protected JsonResult MyJson(object data, JsonRequestBehavior behavior,string format) { return new CustomJsonResult { Data = data, JsonRequestBehavior = behavior, FormateStr = format }; } /// <summary> /// 返回JsonResult42 /// </summary> /// <param>数据</param> /// <param>数据格式</param> /// <returns>Json</returns> protected JsonResult MyJson(object data, string format) { return new CustomJsonResult { Data = data, FormateStr = format }; }

最后我们在自己的Controller中调用即可

public class ProjectMileStoneController : BaseController { /// <summary> /// 首页视图 /// </summary> /// <returns>视图</returns> public ActionResult Index() { return this.View(); } #region 项目里程碑查询 /// <summary> /// 根据项目编号获取项目里程碑 /// </summary> /// <param>项目编号</param> /// <returns>项目里程碑</returns> public JsonResult GetProjectMileStoneByProjectId(int projectId) { IList<ProjectMileStone> projectMileStones = FacadeContainer.Get<IProjectMileStoneService>().GetProjectMileStonesByProjectId(projectId); return this.MyJson(projectMileStones, "yyyy.MM.dd"); } #endregion }

原文地址:

以上就是Asp.net Mvc返回JsonResult中DateTime类型数据格式问题的解决方法,希望对大家的学习有所帮助。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjypyx.html