【发布时间】:2013-07-30 20:46:20
【问题描述】:
我有一个 MVC 应用程序,我正在尝试将它放在一起,它需要一些选择列表和下拉列表。
所以,我有以下模型......
public class Task
{
public int ID { get; set; }
public string Title { get; set; }
......
public virtual ICollection<Monitor> Monitors { get; set; }
public virtual ICollection<Resource> Resources { get; set; }
}
public class Monitor
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public IList<Task> Tasks { get; set; }
}
public class Resource
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
.....
public IList<Task> Tasks { get; set; }
有趣的部分是,当我显示任务列表时,在显示正常的其他属性中,我需要显示分配给任务的“监视器”列表和“资源”列表索引视图如下所示。
@model IEnumerable<ResourceManager.Models.Task>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
.....
<th>
@Html.DisplayNameFor(model => model.Title)
</th>
.....
<th>
@Html.DisplayNameFor(model => model.Monitors)
</th>
<th>
@Html.DisplayNameFor(model => model.Resources)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
.....
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
.....
<td>
@if (item.Monitors == null || item.Monitors.Count == 0)
{<span>No Monitors Assigned</span>}
else
{ string.Join(", ", item.Monitors.Select(m => string.Format("{0} {1}", m.FirstName, m.LastName))); }
</td>
<td>
@Html.DisplayFor(modelItem => item.Resources)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
这里是控制器....
public ActionResult Index()
{
var tasks = from t in db.Tasks where t.IsActive == true select t;
return View(tasks);
}
我希望监视器列表和资源列表在索引、删除和详细信息视图上显示为字符串,即“监视器 1、监视器 2、监视器 3”和“资源 1、资源 2、资源 3” '。
但是在其他视图(创建和编辑)上,我希望它们显示为可选列表。
【问题讨论】:
-
你想在哪里存储选定的监视器或选定的资源?
-
我有一个用于监视器、任务和资源的表,其中包含用于 TaskMonitor 和 TaskResources 的交叉链接表
标签: asp.net asp.net-mvc entity-framework