我还在尝试使用ViewModels和IEnumerable / List作为属性。我显然遗漏了一些不允许我在我的视图中看到我的IEnumberable的东西。
我最终要做的是有一个视图,该视图将显示未分配给组的未分配用户列表(在这种情况下称为Patrols,PatrolId = 0)。该表将在每个成员旁边有一个复选框。该表上方将是可用巡逻的DropDownList。管理员将进入该页面以查看尚未分配的内容,从顶部的DDL中选择一个巡逻,检查他想要的用户分配给该巡逻队,然后提交将要从DDL传递PatrolID并找到所有选定的成员,以便用该PatrolId更新他们的记录。
在下面的视图中,当我循环浏览Scout属性时,我会假设我可以在这部分中使用Model.Scout来循环并写出该IEnumerable中的所有成员。
@for (var i = 0; i < Model.Count(); i++)
然而,当我尝试使用intellisense在使用模型时看到Scout属性时,我没有看到该属性。除此之外,我认为在写出所有行之前,我需要首先检查Scout属性以查看是否有计数。同样,我无法看到Scout属性,以便检查它的数量。
视图模型
public class PatrolMemberViewModel
{
[Key]
public int MemberId { get; set; }
public int PatrolId { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Phone")]
public string PhonePrimary { get; set; }
[Display(Name = "Email")]
public string EmailPrimary { get; set; }
public bool IsSelected { get; set; }
}
public class PatrolUnassignedViewModel
{
public SelectList Patrols { get; set; }
public IEnumerable<PatrolMemberViewModel> Scout { get; set; }
}
控制器
// GET:
public ViewResult Unassigned()
{
PatrolUnassignedViewModel unassinged = new PatrolUnassignedViewModel();
unassinged.Patrols = new SelectList(repository.SelectAllPatrols());
unassinged.Scout = repository.SelectAllUnassigned();
return View(unassinged);
}
存储库
public IEnumerable<PatrolMemberViewModel> SelectAllUnassigned()
{
using (DataContext db = new DataContext())
{
var results = (from p in db.Person
where p.IsActive == true
&& p.IsScout == true
&& p.PatrolId == 0
select new PatrolMemberViewModel
{
MemberId = p.PID,
FirstName = p.FirstName ?? string.Empty,
LastName = p.LastName ?? string.Empty,
EmailPrimary = p.EmailPrimary ?? string.Empty,
PhonePrimary = p.PhonePrimary ?? string.Empty,
PatrolId = p.PatrolId,
IsSelected = false
}
).OrderBy(o => o.LastName).ThenBy(o => o.FirstName).ToList();
return results;
}
}
public IEnumerable<PatrolName> SelectAllPatrols()
{
return db.PatrolNames;
}
查看
@model IList<ProjectName.ViewModels.PatrolUnassignedViewModel>
@{
ViewBag.Title = "Unassigned";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Patrols</h2>
<h4>Assign Scouts to a Patrol.</h4>
@using (Html.BeginForm("Update", "Patrol", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(false, "", new { @class = "alert alert-danger" })
<table class="table table-bordered table-striped table-hover table-condensed tbackground">
<tr>
<th class="text-center">
</th>
<th class="text-center">
First Name
</th>
<th class="text-center">
Last Name
</th>
<th class="text-center">
Email
</th>
<th class="text-center">
Phone
</th>
</tr>
@for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td class="text-center">
@Html.CheckBoxFor(m => m[i].IsSelected)
</td>
<td>
@Html.DisplayFor(m => m[i].FirstName)
</td>
<td>
@Html.DisplayFor(m => m[i].LastName)
</td>
<td>
<a href="mailto:@Model[i].EmailPrimary">@Model[i].EmailPrimary</a>
</td>
<td class="text-center">
@Html.DisplayFor(m => m[i].PhonePrimary)
</td>
</tr>
}
</table>
<div class="control-wrapper">
<input type="submit" id="btnSubmit" value="Assign Scouts" class="btn btn-success" />
</div>
}
<p> </p>
答案 0 :(得分:3)
您的控制器正在返回PatrolUnassignedViewModel类的单个实例
public ViewResult Unassigned()
{
PatrolUnassignedViewModel unassinged = new PatrolUnassignedViewModel();
unassinged.Patrols = new SelectList(repository.SelectAllPatrols());
unassinged.Scout = repository.SelectAllUnassigned();
return View(unassinged);
}
您的观点期待IList
@model IList<ProjectName.ViewModels.PatrolUnassignedViewModel>
应该期待什么
@model ProjectName.ViewModels.PatrolUnassignedViewModel
你的侦察兵是IEnumerable,因此没有计数方法,所以应该是
public IList<PatrolMemberViewModel> Scout { get; set; }
public IList<PatrolMemberViewModel> SelectAllUnassigned()
{
}
你应该像这样做你的循环
@for (var i = 0; i < Model.Scout.Count(); i++)
{
<tr>
<td class="text-center">
@Html.CheckBoxFor(m => m.Scout[i].IsSelected)
</td>
<td>
@Html.DisplayFor(m => m.Scout[i].FirstName)
</td>
<td>
@Html.DisplayFor(m => m.Scout[i].LastName)
</td>
<td>
<a href="mailto:@Model.Scout[i].EmailPrimary">@Model.Scout[i].EmailPrimary</a>
</td>
<td class="text-center">
@Html.DisplayFor(m => m.Scout[i].PhonePrimary)
</td>
</tr>
}
我在这里错过了什么吗?
答案 1 :(得分:1)
从您的GET操作方法,您传递的是PatrolUnassignedViewModel
的单个对象。但是您的视图绑定到PatrolUnassignedViewModel
的集合。所以改变你的观点就像
@model PatrolUnassignedViewModel
现在您可以使用Model.Scout属性作为集合。
@model PatrolUnassignedViewModel
<h2>Total : @Model.Scout.Count()</h2>
@foreach(var item in Model.Scout)
{
<label>@item.FirstName</label>
}