1

I have an MVC app that I am trying to put together that requires some select lists and drop down lists.

So, I 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 interesting part is that when I display a list of tasks, among the other properties that display just fine, I need to display a list of 'Monitors' and a list of 'Resources' that are assigned to the task in the Index view shown below.

@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 here is the controller....

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

I would like for the list of Monitors and the list of Resources to display as a string on the Index, Delete and Details Views i.e. 'Monitor 1, Monitor 2, Monitor 3' and 'Resource 1, Resource 2, Resource 3'.

However on the other views (Create and Edit), I want them to appear as a selectable list.

4

3 回答 3

6

首先在您的控制器中创建选择列表,

   var monitor = //你的 linq 查询

    ViewData["ddlList"] = 监视器 .Select(x => new SelectListItem {
       文本 = x.FirstName,
       值 = x.Id.ToString()
    }).ToList();

然后你可以在你的视图中使用它,如下所示,

<%=Html.DropDownList("myList") %>
于 2013-07-30T17:07:01.060 回答
4

对于监视器/资源的显示(并且由于您希望它们显示为逗号分隔的列表),您可以使用string.Join

<td>
    @string.Join(",", Model.Monitors.Select(m => string.Format("{0} {1}", m.FirstName, m.LastName)))
</td>

为了能够实际使用Html.DisplayFor,您必须创建一个自定义显示模板,以便 Razor 真正知道如何响应。为此,在您的 Views 文件夹中,创建名为“DisplayTemplates”的新文件夹,并在其中创建一个名为“Monitors.cshtml”和“Resources.cshtml”的新局部视图,分别强类型为IEnumerable<Monitor>IEnumerable<Resource>。然后在该文件中,您只需添加与上面大致相同的代码:

@model IEnumerable<Monitor>

@string.Join(",", Model.Select(m => string.Format("{0} {1}", m.FirstName, m.LastName)))

然后在您看来,您可以调用:

@Html.DisplayFor(m => m.Monitors, "Monitors")

不幸的是,在此示例中,您必须提供模板名称,因为DisplayFor列表的默认行为是多次呈现显示模板,列表的每个成员一次。您可以执行以下操作:

# Monitor.cshtml

@model Monitor

@Model.FirstName @Model.LastName,

接着:

@Html.DisplayFor(m => m.Monitors)

但是你的最后一个项目最后会有一个逗号。

要编辑列表,您所要做的就是将选择列表传递给您的视图。最佳情况下,您会使用视图模型来执行此操作,但为简单起见,我将仅ViewBag在此处使用。在您的控制器操作中:

ViewBag.MonitorChoices = db.Monitors.Select(m => new SelectListItem
    {
        Value = m.ID.ToString(),
        Text = string.Format("{0} {1}", m.FirstName, m.LastName)
    });

然后,在您的创建/编辑视图中:

@Html.ListBoxFor(m => m.Monitors, ViewBag.MonitorChoices)
于 2013-07-30T17:13:11.457 回答
0

尝试如下,

var monitor = //你的 linq 查询

    列表监视器列表 = 新列表();
    foreach(监视器中的 var 监视器){
        SelectListItem 项 = 新的 SelectListItem();
        item.Text = monitor.FirstName;
        item.Value = monitor.Id.ToString();
        监视器列表 .Add(item);
    }


ViewData["ddlList"] = monitorList;
于 2013-08-06T05:05:22.523 回答