Automatic Creating of Views with dropdown on 1:n relations
I followed a Tutorial a few weeks ago which shows how to create a simple ASP.NET MVC 3 App (http://www.asp.net/mvc/tutorials/mvc-music-store-part-1). Now I created another app that is actually doing something very simple, but I cant make Visual Studio 2010 automatically creating a View that shows a selection of the 1:n connection. The program is a simple News system with a relation NewsEntry.NewsCategory to NewsCa开发者_如何转开发tegory.ID.
NewsEntry.cs
public class NewsEntry
{
public int ID { get; set; }
public string Title { get; set; }
public string ShortText { get; set; }
public string Text { get; set; }
public DateTime PublishDate { get; set; }
public DateTime UnpublishDate { get; set; }
public NewsCategory NewsCategory { get; set; }
}
NewsDB.cs
public class NewsCategory
{
public int ID { get; set; }
public string Name { get; set; }
public List<NewsEntry> News { get; set; }
}
NewsDB.cs
public class NewsDB : DbContext
{
public DbSet<NewsEntry> NewsEntry { get; set; }
public DbSet<NewsCategory> NewsCategory { get; set; }
}
So my question is what is missing that VS is not creating a view with category in a drop down list?
It obviously helped to add a NewsCategoryId field:
public class NewsEntry
{
public int NewsEntryId { get; set; }
public string Title { get; set; }
public string ShortText { get; set; }
public string Text { get; set; }
public DateTime PublishDate { get; set; }
public DateTime UnpublishDate { get; set; }
public int NewsCategoryId { get; set; }
public virtual NewsCategory NewsCategory { get; set; }
}
精彩评论