使用的模型如下所示
public class EmployeeAlconDetailsEditorViewModel
{
public int EMP_NO { get; set; }
public string EMP_NAME { get; set; }
public string EMP_ROLE_CODE { get; set; }
}
public class ListEmployeeAlconDetails
{
public List<EmployeeAlconDetailsEditorViewModel> listEmployee;
public ListEmployeeAlconDetails()
{
this.listEmployee = new List<EmployeeAlconDetailsEditorViewModel>();
}
public IEnumerable<EmployeeAlconDetailsEditorViewModel> getSelectedIds()
{
// Return an Enumerable containing the Id's of the selected people:
return (from p in this.listEmployee where p.IsSelected select p).ToList();
}
}
通过此控制器显示视图,其中包含复选框。
public ActionResult UploadAlconFile(HttpPostedFileBase UploadedFile)
{
if (UploadedFile != null && UploadedFile.ContentLength > 0)
{
List<TEMP_EMP_DETAILS_ALCON> listAdd = objTempAlcon.NewAlconDetails();
var model = new ListEmployeeAlconDetails();
foreach (TEMP_EMP_DETAILS_ALCON emp in listAdd)
{
var alconDetails = new EmployeeAlconDetailsEditorViewModel()
{
IsSelected = true,
EMP_NO = emp.EMP_NO,
EMP_NAME = emp.EMP_NAME,
};
model.listEmployee.Add(alconDetails);
}
return View("AlconFileUploadView", model);
}
return View("AlconFileUploadView", null);
}
显示表格的视图。
@model ALCONDashboardPortal.ViewModels.ListEmployeeAlconDetails
@using (Html.BeginForm("PopUpSubmit", "ALCON", FormMethod.Post, new { encType = "multipart/form-data"}))
{
<div class="modal-header">
<button class="close" aria-hidden="true" type="button" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body" id="modalContent">
<table class="table table-condensed table-striped">
@Html.EditorFor(model => model.listEmployee)
</table>
</div>
<div class="modal-footer">
<button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
<input class="btn btn-primary" type="submit" name="submit" value="Save Changes" />
</div>
EditorViewModel temlate
@model ALCONDashboardPortal.ViewModels.EmployeeAlconDetailsEditorViewModel
<tr>
<td>
@Html.CheckBoxFor(model => model.IsSelected)
</td>
<td>
@Html.DisplayFor(model => model.EMP_NO)
</td>
<td>
@Html.DisplayFor(model => model.EMP_NAME)
</td>
</tr>
以下代码是提交表单后的操作。我正在获取模型对象但是计数为0,即表单提交后没有任何内容。
public ActionResult PopUpSubmit(ListEmployeeAlconDetails model)
{
var selectedIds = model.getSelectedIds();
// Use the ids to retrieve the records for the selected people
// from the database:
// Redirect somewhere meaningful (probably to somewhere showing
// the results of your processing):
return RedirectToAction("Index");
}