将日期格式转换为

时间:2015-10-15 07:32:08

标签: asp.net-mvc

嗨Guys / Girls我是asp.net mvc的新手,我试图将我的日期转换为可读格式。目前它看起来像这样。

/Date(1444892163827)/

我的模特

public class Movie
    {
        internal string title;
        internal DateTime releasedate;
        internal string genre;

        public int ID { get; set; }

        [Required]
        public string Title { get; set; }

        public DateTime ReleaseDate { get; set; }

        [Required]
        public string Genre { get; set; }
    }

    public class MovieDBContext : DbContext
    {
        public DbSet<Movie> Movies { get; set; }
    }

我的控制器

public JsonResult getAll()
        {
            using (MovieDBContext dataContext = new MovieDBContext())
            {
                var movieList = dataContext.Movies.ToList();
                return Json(movieList, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult getMovieByNo(string MovNo)
        {
            using (MovieDBContext dataContext = new MovieDBContext())
            {
                int no = Convert.ToInt32(MovNo);
                var movieList = dataContext.Movies.Find(no);
                return Json(movieList, JsonRequestBehavior.AllowGet);
            }

        }      

真的很感激任何帮助!

1 个答案:

答案 0 :(得分:1)

您需要在MVC中创建CustomJsonResult,例如JSON.Net与IsoDateTimeConverter一起使用,或者如果仅返回JSON,则应将项目MVC更改为WebAPI。

从此https://stackoverflow.com/a/9302054/4599089创建类(方法2),然后在控制器中使用它:

    public JsonResult getAll()
    {
        using (MovieDBContext dataContext = new MovieDBContext())
        {
            var movieList = dataContext.Movies.ToList();
            return new CustomJsonResult(){Data = movieList};
        }
    }

    public JsonResult getMovieByNo(string MovNo)
    {
        using (MovieDBContext dataContext = new MovieDBContext())
        {
            int no = Convert.ToInt32(MovNo);
            var movieList = dataContext.Movies.Find(no);
            return new CustomJsonResult(){Data = movieList};
        }

    }