【发布时间】:2014-06-04 08:20:43
【问题描述】:
我想从两个 EF 表(菜单和菜单项)中获取数据。然后我想将结果放入 ViewModel。所以我可以轻松地循环浏览 Razor 视图中的数据。
我已经开始工作了。但我是 .Net MVC 的新手,所以我想知道我的代码是否可以改进。也许查询可以合并并且更短。而且我正在使用 AsEnumerable(),我不确定这是否是最好的方法。
我认为我的尝试还可以。但我想听听你的想法。欢迎任何关于代码改进的建议。谢谢。
控制器类:
public ActionResult Index(int? id)
{
if (id == null) return RedirectToAction("Index", new { controller = "Menu" });
var menu =
(from m in _db.Menus
join mi in _db.MenuItems on m.Id equals mi.MenuId
where m.Id == id
select m).First();
var menuItems =
(from mi in _db.MenuItems.AsEnumerable()
join m in _db.Menus on mi.MenuId equals m.Id
where m.Id == id
select new MenuItem
{
Id = mi.Id,
Name = mi.Name,
Href = mi.Href,
CssClass = mi.CssClass,
CssId = mi.CssId,
Title = mi.Title,
Weight = mi.Weight
});
var model = new MenuModelView
{
Id = menu.Id,
Name = menu.Name,
CssClass = menu.CssClass,
CssId = menu.CssId,
Deleted = menu.Deleted,
MenuItems = menuItems
};
return View(model);
}
ViewModel 类:
using System.Collections.Generic;
namespace DesignCrew.Areas.Admin.Models
{
public class MenuModelView
{
public int Id { get; set; }
public string Name { get; set; }
public string CssClass { get; set; }
public string CssId { get; set; }
public bool Deleted { get; set; }
public IEnumerable<MenuItem> MenuItems { get; set; }
}
}
剃刀视图:
@model DesignCrew.Areas.Admin.Models.MenuModelView
@{
ViewBag.Title = "Index";
}
<h2>Menu - @Html.DisplayFor(model => model.Name, new { @class = "control-label col-md-2" })</h2>
<p>
@Html.ActionLink("Create New Item", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.MenuItems.First().Id)
</th>
<th>
@Html.DisplayNameFor(model => model.MenuItems.First().Name)
</th>
<th>
@Html.DisplayNameFor(model => model.MenuItems.First().Href)
</th>
<th>
@Html.DisplayNameFor(model => model.MenuItems.First().Title)
</th>
<th>
@Html.DisplayNameFor(model => model.MenuItems.First().CssClass)
</th>
<th>
@Html.DisplayNameFor(model => model.MenuItems.First().CssId)
</th>
<th>
@Html.DisplayNameFor(model => model.MenuItems.First().ParentId)
</th>
<th>
@Html.DisplayNameFor(model => model.MenuItems.First().Weight)
</th>
<th>Options</th>
</tr>
@foreach (var item in Model.MenuItems)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Href)
</td>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.CssClass)
</td>
<td>
@Html.DisplayFor(modelItem => item.CssId)
</td>
<td>
@Html.DisplayFor(modelItem => item.ParentId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Weight)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "link-menu" }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { @class = "link-menu" })
</td>
</tr>
}
</table>
【问题讨论】:
-
鉴于您似乎在 Viewmodel 中重新使用您的 EF 实体,您不需要明确地将
new和map(select new MenuItem {}...) 实体克隆到ViewModel- 只需使用 EF 引用的那些。此外,即使您确实有类似的专用ViewModels,像AutoMapper这样的工具也可以减轻手动映射的痛苦。 -
另外,你可能不需要
AsEnumerable() -
你能提供一些例子吗?
-
不确定这是否有帮助,但我回答了类似的菜单相关问题。这是Link
标签: c# asp.net-mvc linq razor