MVC and Entity Framework Select List

asp.net asp.net-mvc entity-framework

Question

I am attempting to build an MVC application that needs certain select lists and drop down lists.

I therefore have the following models.

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; }

The intriguing thing is that, in the Index view, which is shown below, I need to display a list of "Monitors" and a list of "Resources" that are assigned to the job in addition to the other properties, which appear perfectly fine.

@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>

And the controller is right here.

    public ActionResult Index()
    {
        var tasks = from t in db.Tasks where t.IsActive == true select t;
        return View(tasks);
    }

As in "Monitor 1, Monitor 2, Monitor 3" and "Resource 1, Resource 2, Resource 3," I would like the list of Monitors and the list of Resources to appear as a string in the Index, Delete, and Details Views.

But I want them to show up as a selectable list on the other views (Create and Edit).

1
1
8/5/2013 5:49:32 PM

Popular Answer

ZZZ_tmp
6
7/30/2013 5:07:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow