Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables: Category with Id,Name and Entry with 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:
var query = from cat in _db.Categories
                      join en in _db.Entries on cat.Id equals en.CategoryId
          select new{
            CategoryId=cat.Id,
            Title = en.Title,
            Username =en.Username,
            Password =en.Password,
            Url = en.Url,
            Description = en.Description,
            Category=cat.Name
          };

           return View(query);


I have trouble how to use it in View. Any Help?

my View:
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Username)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Password)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Url)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
           // Here should be Category
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Username)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Password)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Url)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
             // Here should be Category
        </td>
    
    </tr>
}

</table>
Posted
Updated 5-Oct-14 23:20pm
v3
Comments
Pheonyx 6-Oct-14 5:56am    
What happens when you do: @Html.DisplayFor(modelItem=>item.Category) ?

1 solution

you lack a ViewModel

your anonymous
C#
select new{
            CategoryId=cat.Id,
            Title = en.Title,
            Username =en.Username,
            Password =en.Password,
            Url = en.Url,
            Description = en.Description,
            Category=cat.Name
          };


should be an entity

for example a class called EntriesWithCateogories
C#
public class EntriesWithCateogories
{
 public int CateogiryId {get;set;}
 public string Category {get;set;}
 public string Title {get;set;}
 ...
} 


then your query becomes

C#
var query = from cat in _db.Categories
                       join en in _db.Entries on cat.Id equals en.CategoryId
                       select new EntriesWithCateogories{
            CategoryId=cat.Id,
            Title = en.Title,
            Username =en.Username,
            Password =en.Password,
            Url = en.Url,
            Description = en.Description,
            Category=cat.Name
          };
return View(query.ToList());


and your View NEED at the top
C#
@model IEnumerable<EntriesWithCategories>



on the other hand you can create a real ViewModel that contains the list
ex :
C#
public class EntriesCategoryViewModel {
 public string PageTitle {get;set;}
 public List<EntriesWithCategories> List {get;set;}
}

and your controller code becomes
C#
var model = new EntriesCategoryViewModel();
model.Title = " my page title"; 
model.List = query.ToList();
return View(model);


and the view

C#
@model EntriesCategoryViewModel



@foreach (var item in Model.List)
{
    <div>@item.Category</div>

}
 
Share this answer
 
v4
Comments
PetarS089 6-Oct-14 8:26am    
Thank you for the great answer and explanation @nrgjack, i now understand where i was wrong and what i was missing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900