我有一个绑定到视图的对象。该对象具有一个带有布尔字段的项目列表。我使用此布尔值字段绑定到表单上的复选框。我希望用户选择他们想要的项目,然后提交。问题是,当它提交并传递到后期操作时,项目列表为空。
这是传递给视图的视图模型。
public class ReAssignVM
{
public string ToUsername { get; set; }
public string FromUsername { get; set; }
public IEnumerable<ReAssignTaskVM> Tasks { get; set; }
} // End of Class
这是视图:
@model SRM.ClassLibrary.V1.ViewModels.TaskViewModels.ReAssignVM
@{
ViewData["Title"] = "Re-Assign Tasks";
}
<h2>Re-Assign Tasks</h2>
<br />
<div class="row">
<div class="col-md-1 alignRight">
<b>From:</b>
</div>
<div class="col-md-3">
@Model.FromUsername
</div>
<div class="col-md-1 alignRight">
<b>To:</b>
</div>
<div class="col-md-3">
@Model.ToUsername
</div>
</div>
<br />
<form asp-action="ReAssignTasks">
<input type="hidden" asp-for="FromUsername" />
<input type="hidden" asp-for="ToUsername" />
<div include-if="@Model.Tasks != null && Model.Tasks.Count() > 0">
<table class="table table-hover">
<thead class="thead-light">
<tr>
<th></th>
<th>
Status
</th>
<th>
Date
</th>
<th>
Student
</th>
<th>
Description
</th>
</tr>
</thead>
<tbody>
@foreach (var task in Model.Tasks.OrderBy(n => n.TaskDate))
{
<tr>
<td>
@Html.HiddenFor(item => task.TaskID)
@Html.CheckBoxFor(item => task.Assign)
</td>
<td>
@Html.DisplayFor(item => task.TaskStatus)
</td>
<td>
@task.TaskDate.ToLocalTime().ToString("M/d/yyyy")
</td>
<td>
@Html.DisplayFor(item => task.StudentUsername)
</td>
<td>
@Html.DisplayFor(item => task.Description)
</td>
</tr>
}
</tbody>
</table>
<br />
<div class="row">
<div class="col-md-12 alignRight">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#assignment">
Re-Assign
</button>
</div>
</div>
</div>
<div class="modal fade" id="assignment" tabindex="-1" role="dialog" aria-labelledby="assignmentLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="assignmentLabel">Confirm Re-Assignment of Tasks?</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Selected tasks will be re-assigned from @Model.FromUsername to @Model.ToUsername</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<input type="submit" value="Confirm" class="btn btn-primary" />
</div>
</div>
</div>
</div>
</form>
<div exclude-if="@Model.Tasks != null && Model.Tasks.Count() > 0">
<div class="alert alert-info">
There are no tasks to re-assign.
</div>
</div>
有人可以帮助我了解如何正确执行此操作吗?
答案 0 :(得分:1)
您将需要为您的Collection项目提供索引,以将其回发到模型中,因为Model活页夹将使用那些将对象绑定回行动。因此,您应该在视图中使用For循环和IList<T>
作为模型:
@for(int i=0; i< Model.Tasks.Count(); i++)
{
@Html.HiddenFor(item => Model.Tasks[i].TaskID)
@Html.CheckBoxFor(item => Model.Tasks[i].Assign)
............
............
}
,然后在模型中将其更改为IList<T>
,因为您在IEnumerable<T>
上没有索引器:
public class ReAssignVM
{
public string ToUsername { get; set; }
public string FromUsername { get; set; }
public IList<ReAssignTaskVM> Tasks { get; set; }
} // End of Class