如何在View MVC4中显示相关表的数据 [英] How to display data from related tables in View MVC4

查看:64
本文介绍了如何在View MVC4中显示相关表的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表:带有标题,名称和条目的类别,名称,用户名,密码,CategoryId。所以我希望输出是列出的所有条目,但最后是Category.Name。 EX。



ID标题名称用户名密码类别

------------------ -------------------------------------------



1 Facebook Peter Batman 123456社交网络



所以我有索引方法返回列表:

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);





我无法在View中使用它。任何帮助?



我的观点:



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>

推荐答案

你缺少ViewModel



你的匿名

you lack a ViewModel

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





应该是一个实体



例如一个名为EntriesWithCateogories的类



should be an entity

for example a class called EntriesWithCateogories

public class EntriesWithCateogories
{
 public int CateogiryId {get;set;}
 public string Category {get;set;}
 public string Title {get;set;}
 ...
} 





然后您的查询变为





then your query becomes

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

@model IEnumerable<EntriesWithCategories>







另一方面你可以创建一个包含列表的真实ViewModel

ex:




on the other hand you can create a real ViewModel that contains the list
ex :

public class EntriesCategoryViewModel {
 public string PageTitle {get;set;}
 public List<EntriesWithCategories> List {get;set;}
}



和您的控制器代码变为


and your controller code becomes

var model = new EntriesCategoryViewModel();
model.Title = " my page title"; 
model.List = query.ToList();
return View(model);





和视图





and the view

@model EntriesCategoryViewModel



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

}


这篇关于如何在View MVC4中显示相关表的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆