MVC和实体框架选择列表 [英] MVC and Entity Framework Select List

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

问题描述



所以,我有以下模型... 。

  public class Task 
{
public int ID {get;组; }
public string标题{get;组; }
......
public virtual ICollection< Monitor>监视器{get;组; }
public virtual ICollection< Resource>资源{get;组;

}
public class Monitor
{
public int ID {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }

public IList< Task>任务{get;组; }
}
public class Resource
{
public int ID {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
.....

public IList< Task>任务{get;组; }

有趣的部分是当我显示任务列表时,显示的其他属性好的,我需要在下面的索引视图中显示一个监视器列表和一个分配给任务的资源列表。

  @model IEnumerable< ResourceManager.Models.Task> 

@ {
ViewBag.Title =索引;
}

< h2>索引< / h2>

< p>
@ Html.ActionLink(创建新,创建)
< / 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){
< 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(编辑,编辑,新的{id = item.ID})|
@ Html.ActionLink(Details,Details,new {id = item.ID})|
@ Html.ActionLink(删除,删除,新的{id = item.ID})
< / td>
< / tr>
}
< / table>

这里是控制器....

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

我想要显示器列表和资源列表显示为索引,删除和详细信息视图上的字符串,即监视器1,监视器2,监视器3和资源1,资源2,资源3。



其他视图(创建和编辑),我希望它们显示为可选列表。

解决方案

首先创建您的控制器,

 

var monitors = //您的linq查询

ViewData [ddlList] = monitor。选择(x => new SelectListItem {
Text = x.FirstName,
Value = x.Id.ToString()
})ToList();

然后,您可以在以下视图中使用它,

 <%= Html.DropDownList(myList)%> 


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.

解决方案

First Create Select list in your controller,


   var monitors = //Your linq query

    ViewData["ddlList"] = monitors .Select(x => new SelectListItem {
       Text = x.FirstName, 
       Value = x.Id.ToString() 
    }).ToList();

And then you can use it in your view as follows,

<%=Html.DropDownList("myList") %>

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

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