0

Hi Guys/Girls Im super new to asp.net mvc and Im trying to convert my date to a readable format. Currently it looks like this.

/Date(1444892163827)/

My Model

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

My Controller

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

        }      

Any help would really be appreciated !

EugeneDeWaal
  • 216
  • 2
  • 15
  • Refer [this answer](http://stackoverflow.com/questions/206384/format-a-microsoft-json-date) or just return a collection of anonymous objects with the `DateTime` converted to a formatted `string` –  Oct 15 '15 at 07:34
  • To return anonymous objects it would be `var movieList = dataContext.Movies.Select(m => new { ID = m.ID, Title = m.Title, ReleaseDate = m.ReleaseDate.ToString(yourFormat), Genre = m.Genre });` –  Oct 15 '15 at 08:08
  • Hi Stephen, thank you for your response this also did work :D – EugeneDeWaal Oct 15 '15 at 12:20

1 Answers1

1

You need create CustomJsonResult in MVC where you use for example JSON.Net with IsoDateTimeConverter or you should change your project MVC to WebAPI if you return only JSON.

Create class from this https://stackoverflow.com/a/9302054/4599089 (Approach 2) and then use this in controller:

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

    }  
Community
  • 1
  • 1
Bartosz Czerwonka
  • 1,631
  • 1
  • 10
  • 11