列出C#MVC下的所有项目

时间:2017-09-06 14:32:09

标签: c# asp.net-mvc

我想知道如何能够列出视图中某个类别下的所有项目。我已经四处搜索,无法找到任何内容,如果有人知道任何其他帖子或文章,请指出我。其他明智的是我的模特......

public class Category
{
    [Key]
    public int ID { get; set; }
    [Display(Name = "Category")]
    public string Name { get; set; }
    public virtual ICollection<MenuItem> MenuItem { get; set; }
}

public class MenuItem
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }
    public string Image { get; set; }
    public int CategoryID { get; set; }
    public virtual Category Category { get; set; }
}

感谢任何人提供的任何帮助。

3 个答案:

答案 0 :(得分:1)

@foreach (var item in Model.MenuItem)
{
<tr>
    <td>@Name</td>
    <td>@Description</td>     
</tr>
{

答案 1 :(得分:0)

假设您的类别设置为模型

@foreach(var item in Model.MenuItem)
{
    @Html.HiddenFor(x => item.ID)
    <div>@Html.DisplayFor(x => item.Name)</div>
    // carry on with other properties / markup
}

答案 2 :(得分:0)

假设您想要遍历MenuItem模型中的Category媒体资源,在您看来,您有以下内容:

在视图的顶部,你有这个:
@model Category

@foreach (var item in Model.MenuItem)
   {
      <tr id="menu-item">
        <td>@id</td>
        <td>@Name</td>
        <td>@Description</td>
        <td>@Image</td>
      </tr>
   }
相关问题