MVC和Entity Framework选择列表 [英] MVC and Entity Framework Select List

查看:202
本文介绍了MVC和Entity Framework选择列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想放在一起,需要一些选择列表和下拉列表的MVC应用程序。

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>

这是控制器....

    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

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.

推荐答案

有关监视/资源(因为你希望它们显示为一个逗号分隔的列表),你可以使用字符串的显示。加入

For the display of Monitors/Resources (and since you want them displayed as a comma-delimited list), you can just use string.Join:

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

要能够真正使用 Html.DisplayFor ,你必须创建一个自定义显示模板,以便剃刀就知道该如何应对。要做到这一点,在你的浏览文件夹,创建一个名为DisplayTemplates新文件夹,并在这,创建一个名为Monitors.cshtml和Resources.cshtml一个新的局部视图,强类型为的IEnumerable&LT ;显示器和GT; 的IEnumerable&LT;资源与GT; ,分别为。然后,该文件中,你只需要添加大致相同code如上:

To be able to actually use Html.DisplayFor, you'd have to create a custom display template so Razor will actually know how to respond. To do so, in your Views folder, create new folder called "DisplayTemplates", and in that, create a new partial view called "Monitors.cshtml" and "Resources.cshtml", strongly-typed to IEnumerable<Monitor> and IEnumerable<Resource>, respectively. Then inside that file, you'll just add roughly the same code as above:

@model IEnumerable<Monitor>

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

然后在您的视图中,您可以拨打:

Then in your view, you can call:

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

不幸的是,在这个例子中,你必须养活模板名称,因为 DisplayFor 对列表中的默认行为,是对显示模板多次渲染,一旦为列表中的每个成员。您的 的可以这样做:

Unfortunately, in this example, you'd have to feed the template name because the default behavior of DisplayFor for a list, is to render the display template multiple times, once for each member of the list. You could do something like:

# Monitor.cshtml

@model Monitor

@Model.FirstName @Model.LastName,

和则:

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

但是,你的最后一个项目就必须在最后一个逗号。

But your last item would have a comma at the end.

有关编辑列表,所有你需要做的就是通过选择列表中你的看法。理想情况下,你应该这样做,以期模式,但为了简单起见,我只使用 ViewBag 在这里。在你的控制器动作:

For editing the lists, all you have to do is pass the select lists to your view. Optimally, you'd do this with a view model, but for simplicity's sake, I'll just use ViewBag here. In your controller action:

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

然后,在你创建/编辑观点:

Then, in your create/edit view:

@Html.ListBoxFor(m => m.Monitors, ViewBag.MonitorChoices)

这篇关于MVC和Entity Framework选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆