McKeymayker
McKeymayker

Reputation: 358

How to display data from related tables in View MVC4?

I have two tables:

Category
=========
Id
Name 


Entry
=======
Title
Name
Username
Password
CategoryId. 

So I want the output to be All Entries listed but with Category.Name at the end. EX.

ID    Title     Name           Username     Password     Category
===   =====     ========       =========    =========    ===========
1     Facebook  Peter Batman   123456       Social        Network

So i have the Index method that returns list:

public ActionResult Index()
    {
       IEnumerable<ListAllEntriesViewModel> query = null;
        query = (from cat in _db.Categories
                 join en in _db.Entries on cat.Id equals en.CategoryId
                 select new
                 {
                     Id = cat.Id,
                     Title = en.Title,
                     Username = en.Username,
                     Password = en.Password,
                     Url = en.Url,
                     Description = en.Description,
                     CategoryName = cat.Name
                 }).AsEnumerable();

        return View(query);
    }

I have trouble converting it to IEnumerable. Hhere is an error which cannot figure solution:

 Error  1   Cannot implicitly convert type    
'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 
'System.Collections.Generic.IEnumerable<eManager.Web.Models.ListAllEntriesViewModel>'. 
 An explicit   conversion exists (are you missing a cast?)

Any Help?

Upvotes: 2

Views: 380

Answers (2)

Coding Flow
Coding Flow

Reputation: 21881

As the error says you are returning an IEnumerable of an anonymous type not of your Viewmodel. Assuming your viewmodel has all the required properties, you need to create a new ListAllEntriesViewModel in your select.

query = (from cat in _db.Categories
                 join en in _db.Entries on cat.Id equals en.CategoryId
                 select new ListAllEntriesViewModel()
                 {
                     Id = cat.Id,
                     Title = en.Title,
                     Username = en.Username,
                     Password = en.Password,
                     Url = en.Url,
                     Description = en.Description,
                     CategoryName = cat.Name
                 });

Upvotes: 1

Neel
Neel

Reputation: 11731

something like this you need to add ListAllEntriesViewModel , because in your code the type is anonymous so make changes as shown in below code :-

query = (from cat in _db.Categories
                 join en in _db.Entries on cat.Id equals en.CategoryId
                 select new ListAllEntriesViewModel
                 {
                     Id = cat.Id,
                     Title = en.Title,
                     Username = en.Username,
                     Password = en.Password,
                     Url = en.Url,
                     Description = en.Description,
                     CategoryName = cat.Name
                 }).AsEnumerable();

Upvotes: 3

Related Questions