Help with displaying master detail data into mvc3 view?

Up vote 0 down vote favorite share g+ share fb share tw.

DateModified { get; set; } public int UserID { get; set; } public int CategoryID { get; set; } public virtual User User { get; set; } public virtual Category Category { get; set; } } public class Category { public int CategoryID { get; set; } Required(ErrorMessage = "Category name is required. ") Display(Name = "Category Name") public string Name { get; set; } public virtual ICollection Pages { get; set; } } and I want to populate this navigation list: @foreach (var pages in Model) { CATEGORY NAME GOES HERE @foreach (var pages in Model) { PAGE NAMES GO HERE } } but I'm having problems implementing the controller. I tried this ViewModel: public class MainPageModels { public Category Categories { get; set; } public Page Pages { get; set; } } but it just confused me even more with this error message: System.Data.Edm.

EdmEntityType: : EntityType 'MainPageModels' has no key defined. Define the key for this EntityType. System.Data.Edm.

EdmEntitySet: EntityType: EntitySet MainModels is based on type MainPageModels that has no keys defined. This is my controller: public ActionResult Index() { var pages = db.MainModels. Select(p => p.

Pages). Select(c => c. Category); return View(pages); } I may be missing something simple here.

Asp.net-mvc asp.net-mvc-3 link|improve this question asked Aug 25 '11 at 5:18Ron22719 88% accept rate.

It seems from your post that you have specified an EntitySet of type MainModels, if you want to do this, you will need to specify a key or id of somesort. – Pieter Germishuys Aug 25 '11 at 6:30 A key in the MainModel class? I've been browsing several thread in SO and only see them declaring the model classes within the main model.

I'm a mvc3 noob. – Ron Aug 25 '11 at 6:36 There are generally 2 types that are used in MVC, Models which generally refers to an object that gets data from a DB or it might be your domain model. Then you additionally have a ViewModel, which is what your view will consume.

You have defined an Entity Data Model, i.e. Looking at how you are accessing the MainModels, it's through the DbContext. This means that Entity framework expects this object to have a key defined For ViewModels, there are generally no database ties.

– Pieter Germishuys Aug 25 '11 at 6:42 Could you please post an example of the id within the viewmodel? – Ron Aug 25 '11 at 6:53.

Posting this here for the code/syntax public class Person { Key public int PersonID { get; set; } public string Name { get; set; } public string LastName { get; set; } } public class DataContext : DbContext { EntitySet Persons { get; set; } } Your View Model can then do the following public class PersonAddViewModel { public string Name { get; set; } public string LastName { get; set; } public void CreateViewModelFromDataModel(Person person) { this.Name = person.Name; this. LastName = person. LastName ; } } This is just an example, just to show the difference between a Data Model and a View Model Your View would then be a strongly typed view of PersonAddViewModel.

Thanks for this. How about a ViewModel that uses two data models with a one-to-many relationship like person-orders? – Ron Aug 26 '11 at 1:40 I finally got my list to work using some your ideas.

Will post my answer. – Ron Aug 26 '11 at 3:16.

Here my solution to my parent-child list problem: I created a ViewModel to house both my categories and pages: public class HomeViewModels { Key public int HomeViewKey { get; set; } //This is a MUST! Public IEnumerable ViewCategories { get; set; } public IEnumerable ViewPages { get; set; } public void CreateHomeViewModel(IEnumerable categories, IEnumerable pages) { this. ViewCategories = categories; this.

ViewPages = pages; } } Then edited my controller to populate the viewmodel: public ActionResult Index() { HomeViewModels homePages = new HomeViewModels(); homePages. CreateHomeViewModel(db.Categories.ToList(), db.Pages.ToList()); return View(homePages); } and finally creating the ul-li lists with the following: @{var hvCategories = Model. ViewCategories;} @foreach (var categories in hvCategories) { @Html.

ActionLink(categories.Name, "Index", "Home") @{var hvPages = Model. ViewPages . Where(p => p.

CategoryID == categories. CategoryID);} @foreach (var pages in hvPages) { @Html. ActionLink(pages.Name, "Index", "Home") } I hope this helps anyone who plans to build a nested list using a parent-child model.

This took me two days to figure out. Cheers!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions