在Raven DB中查询Json文档 - 传递给MVC视图

时间:2011-07-21 17:04:48

标签: asp.net-mvc json asp.net-mvc-3 ravendb

下面是Raven DB中的Json文档。如何编写查询以获取在此影院中播放的所有电影并将其作为模型传递到MVC中的视图?谢谢你的帮助。

 {
  "TheaterId": "Hd45",
  "TheaterName" : "Blvd",

  {
     "MovieName": "Wild wild west",
     "ReleaseDate": "12th Dec",
     "NumOfShows ": "5"    
  }
  {
     "MovieName": "Shrek",
     "ReleaseDate": "12th Dec",
     "NumOfShows ": "5"    
   }
   {
     "MovieName": "Ronin",
     "ReleaseDate": "12th Dec",
     "NumOfShows ": "5"    
   }
}

1 个答案:

答案 0 :(得分:1)

实际查询应该非常简单...... 假设你有这样的模型:

public class Theater
{
    public string TheaterId { get; set; }
    public string TheaterName { get; set; }

    public List<Movie> Movies { get; set; }

    public class Movie
    {
        public string MovieName { get; set; }
        public string ReleaseDate { get; set; }
        public int NumOfShows { get; set; }
    }
}

...然后乌鸦查询看起来像这样:

    private ActionResult MoviesForTheater(string theaterId)
    {
        var theater = Session.Load<Theater>(theaterId);
        if (theater == null)
            return HttpNotFound("Theater not found!");

        var movies = theater.Movies.Select(movie => movie.MovieName).ToList();
        return View(movies);
    }

但是 - 我想知道为什么你的id-property名为“TheaterId”而不是“Id”,这是默认的RavenDB约定。

您可能希望查看RacconBlog以获得使用RavenDB精心设计的MVC应用程序的一个非常好的示例。