Jay*_*vis 1 asp.net asp.net-mvc entity-framework
我有一个MVC应用程序,我试图放在一起,需要一些选择列表和下拉列表.
所以,我有以下型号....
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; }
Run Code Online (Sandbox Code Playgroud)
有趣的是,当我显示一个任务列表时,在显示正常的其他属性中,我需要显示一个"监视器"列表和一个"资源"列表,这些列表分配给显示的索引视图中的任务下面.
@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>
Run Code Online (Sandbox Code Playgroud)
这是控制器......
public ActionResult Index()
{
var tasks = from t in db.Tasks where t.IsActive == true select t;
return View(tasks);
}
Run Code Online (Sandbox Code Playgroud)
我希望监视器列表和资源列表在索引,删除和详细信息视图中显示为字符串,即"监视器1,监视器2,监视器3"和"资源1,资源2,资源3".
但是在其他视图(创建和编辑)上,我希望它们显示为可选列表.
首先在控制器中创建选择列表,
var monitors = //Your linq query
ViewData["ddlList"] = monitors .Select(x => new SelectListItem {
Text = x.FirstName,
Value = x.Id.ToString()
}).ToList();
然后你可以在你的视图中使用它,如下所示,
<%=Html.DropDownList("myList") %>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12454 次 |
| 最近记录: |