我是MVC Noob,但我真的想獲得一些使用MVC的經驗,所以我嘗試使用MVC重新創建一個Asp Classic項目。下面的代碼不會顯示膳食/索引頁面上與菜單相關的膳食列表。在視圖中顯示模型
但是,我已經閱讀了幾種不同的模式,ninject和自動映射器,但是從我可以告訴它什麼都沒有,這是處理簡單關聯時所必需的。
我只想將菜單添加到菜單並彙總每個菜單的膳食價格。
型號:
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.
}
菜單控制器:
public ViewResult Index()
{
return View(unitOfWork.MenuRepository.Get());
}
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; } }
編輯:
可以顯示'unitOfWork.MenuRepository.Get()'的代碼,以及強類型的視圖是什麼? – christiandev
請勿圍繞實體框架實施存儲庫/工作單元模式。 EF *已經*使用這種模式('DbContext'是你的UoW,你的'DbSet's是你的倉庫)。如果你想抽象使用EF的邏輯(這是一個好主意),那麼應該實現類似服務模式的東西。 –
@ChrisPratt有關如何在EF中集成服務模式的任何鏈接? ,EF通過provind DBSet和DBContext爲您提供了所有事情:如何更好地控制開發人員不允許將對象添加到某些數據集,而不允許在DBSet上執行某些操作? –