【发布时间】:2016-04-13 22:30:01
【问题描述】:
我正在练习这个项目,我需要向用户显示客户列表和每个客户的复选框,以便他可以选择一些然后单击按钮。
我成功地制作了列表,我可以看到列表中每一行都有一个复选框。
问题是:
我不知道如何使每个复选框与列表中的每个项目相关...目前它们就像整个列表的单个复选框一样工作。
我对 C# 很陌生,也许我只是在这里做一些愚蠢的事情,所以你能帮我解决这个问题吗?
另外,我将如何迭代列表以检查选择了哪些?
这是我的代码:
我的视图模型:
public class TransferViewModel
{
public TransferViewModel()
{
Clients = new List<Client>();
}
public virtual ICollection<Client> Clients { get; set; }
[DisplayName("ID")]
public int? Id { get; set; }
[DisplayName("Transferir de")]
public string TransferFrom { get; set; }
[DisplayName("Transferir para")]
public string TransferTo { get; set; }
[DisplayName("Selecionado")]
public bool IsSelected { get; set; }
[DisplayName("Categoria do Cliente")]
public virtual ClientCategory Category { get; set; }
[DisplayName("Categoria do Cliente")]
public int? CategoryId { get; set; }
[DisplayName("Razão Social")]
public string OfficialName { get; set; }
[DisplayName("Nome Fantasia")]
public string DisplayName { get; set; }
[DisplayName("Responsável")]
public string AssignedToLogin { get; set; }
[DisplayName("Excluído")]
public bool IsDeleted { get; set; }
[DisplayName("Ultima atividade")]
public DateTime? LastInteractionOn { get; set; }
}
我的控制器:
[ViewBagListOfUsers]
// GET: ActivityTypes/Create
public ActionResult TransferSpecificClients(TransferViewModel model)
{
var selectedUserFrom = model.TransferFrom;
var selectedUserTo = model.TransferTo;
var query = db.Clients as IQueryable<Client>;
if (selectedUserFrom!=null)
{
model.Clients = query
.Where(p => p.AssignedToLogin == selectedUserFrom)
.ToArray();
}
if ((selectedUserTo != null)&&(selectedUserFrom != null))
{
//do something if the client is selected ...
}
return View( model);
我的看法:
@model EFGEREN.App.Models.ViewModels.Clients.TransferViewModel
@using EFGEREN.App.Models.Entities
@using System.Linq
@using System.Web.Mvc
@using Resources
@{
ViewBag.Title = "TransferSpecificClients";
IEnumerable<SelectListItem> usersListFrom = ViewBag.UserList;
IEnumerable<SelectListItem> usersListTo = ViewBag.UserList;
}
<h2>TransferSpecificClients</h2>
@using (Html.BeginForm())
{
@Html.DisplayNameFor(model => model.TransferFrom)
@Html.DropDownListFor(m => m.TransferFrom, usersListFrom, Expressions.DropDownListSelectOneX, new { @class = "form-control" })
<div class="controls well well-sm">
<button type="submit" value="Filtrar" class="btn btn-success"><i class="glyphicon glyphicon-ok"></i>@("Filtrar")</button>
</div>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.IsSelected)
</th>
<th>
@Html.DisplayNameFor(model => model.CategoryId)
</th>
<th>
@Html.DisplayNameFor(model => model.OfficialName)
</th>
<th>
@Html.DisplayNameFor(model => model.DisplayName)
</th>
<th>
@Html.DisplayNameFor(model => model.AssignedToLogin)
</th>
<th>
@Html.DisplayNameFor(model => model.LastInteractionOn)
</th>
<th></th>
</tr>
@foreach (var item in Model.Clients) {
<tr>
<td>
@Html.CheckBoxFor(m => m.IsSelected, new { @checked = "checked" })
</td>
<td>
@Html.DisplayFor(modelItem => item.CategoryId)
</td>
<td>
@Html.DisplayFor(modelItem => item.OfficialName)
</td>
<td>
@Html.DisplayFor(modelItem => item.DisplayName)
</td>
<td>
@Html.DisplayFor(modelItem => item.AssignedToLogin)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastInteractionOn)
</td>
</tr>}
</table>
@Html.DisplayNameFor(model => model.TransferTo)
@Html.DropDownListFor(m => m.TransferTo, usersListTo, Expressions.DropDownListSelectOneX, new { @class = "form-control" })
<div class="controls well well-sm">
<button type="submit" value="Transferir" class="btn btn-success"><i class="glyphicon glyphicon-ok"></i>@("Transferir")</button>
</div>
}
【问题讨论】:
-
您不能使用
foreach循环(请参阅骗子)。并删除您的 `new { @checked = "checked" }` 代码。CheckBoxFor()方法根据属性的值确定它是否被选中。
标签: c# asp.net-mvc list checkbox foreach