简体   繁体   English

在视图中显示模型

[英]Display model in View

I'm an MVC Noob but I really want to gain some experience using MVC so I am trying recreate an Asp Classic project using MVC. 我是一个MVC Noob,但我真的想获得一些使用MVC的经验,所以我尝试使用MVC重新创建一个Asp Classic项目。 The code below does't not display the list of meals associated with a menu on the Meals/Index page. 下面的代码不会显示与膳食/索引页面上的菜单相关的膳食列表。

I have read about several different patterns, ninject and auto mapper however, from what I can tell it's nothing like that is necessary when dealing with a simple association. 我已经阅读了几个不同的模式,ninject和自动映射器,然而,从我可以告诉它,在处理简单的关联时,这是不必要的。

I would just like to add Meals to a Menu and aggregate the meal prices for each menu. 我只想将菜单添加到菜单中并汇总每个菜单的餐价。

Models: 楷模:

public class Meal
{   [Key]
    public int MealId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public int MenuId { get; set; }
}


public class Menu
{
    [Key]
    public int MenuId { get; set; }
    public DateTime WeekendServed { get; set; }
    public decimal Price { get; set; }
    public List<Model> Meals { get; set; //Menus have meals. 
}

Menu Controller: 菜单控制器:

 public ViewResult Index()
 {
    return View(unitOfWork.MenuRepository.Get());
 }

Menu/Index/ : Menu/Index/

@model IEnumerable<Skimos.Models.Meal>
----

<Table Headers>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.WeekendServed)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @foreach (var meal in item.Meals)
            {
                @Html.Display(meal.Name)
                @Html.Display(meal.Description)
                @Html.Display(meal.Price.ToString())
            }
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.MenuId }) |
            @Html.ActionLink("Details", "Details", new { id=item.MenuId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.MenuId })
        </td>
    </tr>
}

MenuRepository public IQueryable Menus { get { return context.Menus; } } MenuRepository public IQueryable Menus { get { return context.Menus; } } public IQueryable Menus { get { return context.Menus; } } Edit: public IQueryable Menus { get { return context.Menus; } }编辑:

The issue is that EF requires two things for list-type navigation properties: 1) an ICollection type and 2) virtual on the property: 问题是EF对列表类型导航属性需要两件事:1) ICollection类型和2)属性上的virtual

So if you change your Meals property to the following, you should be good: 因此,如果您将Meals属性更改为以下内容,您应该是好的:

public virtual ICollection<Meal> Meals { get; set; }

The reason for ICollection is that the return value from EF will be an IQueryable type. ICollection的原因是EF的返回值是IQueryable类型。 The reason for the virtual is that EF actually returns proxy classes instead of the actual model class. 虚拟的原因是EF实际上返回代理类而不是实际的模型类。 These proxies have overrides for the navigation properties that return the datasets. 这些代理具有覆盖返回数据集的导航属性的覆盖。 If your property is not virtual, EF can't override it. 如果您的属性不是虚拟的,则EF无法覆盖它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM