Lista wyboru MVC i Entity Framework
Mam aplikację MVC, którą próbuję połączyć, która wymaga kilku list wyboru i rozwijanych list.
Mam więc następujące modele ....
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; }
Ciekawostką jest to, że gdy wyświetlam listę zadań, wśród innych właściwości, które wyświetlają się dobrze, muszę wyświetlić listę „Monitory” i listę „Zasobów” przypisanych do zadania w wyświetlonym widoku indeksu poniżej.
@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>
A oto kontroler ....
public ActionResult Index()
{
var tasks = from t in db.Tasks where t.IsActive == true select t;
return View(tasks);
}
Chciałbym, aby lista monitorów i lista zasobów były wyświetlane w postaci ciągu w widoku indeksu, usuwania i szczegółów, tj. „Monitor 1, Monitor 2, Monitor 3” i „Resource 1, Resource 2, Resource 3”.
Jednak w innych widokach (Utwórz i edytuj) chcę, aby pojawiały się jako lista do wyboru.