在ASP.NET CORE 2.0中,我正在创建一个Ajax表单。可以使用此表单更新多名员工。有一个员工编号和一个多选列表框,用于选择员工的报销 问题是如何处理Reimbursement值,因为可以从列表框中选择多个项目。表格将作为键值对提交。如果报销键是报销,则值将是多个。因此,如何检查为哪个EmployeeID选择了报销?
@model myweb.NewWOModel
<form id="frmWODetail" asp-action="EditMultipleEmployee" asp- controller="WOData" data-ajax="true" data-ajax-method="POST" data-ajax-success="onSuccess" data-ajax-failure="onFailed">
@Html.AntiForgeryToken()
<div class="row">
<div class="col-sm-12">
@foreach (var record in Model.MultipleEmployeeModels)
{
<div class="row">
<div class="col-sm-6">
<label>WorkOrder No.:</label>
<span>
@record.EmployeeNo
</span>
</div>
<div class="col-sm-6">
<input type="hidden" name="EmployeeID" value="@record.EmployeeID" />
</div>
</div>
<div class="row col-sm-12">
<select name="Reimbursement" asp-for="@record.ReimbursementID" asp-items="@(new SelectList(Model.ReimbursementModels,"ReimbursementID","ReimbursementName"))" multiple></select>
</div>
<div class="row">
<div class="col-sm-12 text-right">
<input type="submit" class="btn btn-success" value="Save"/>
<input type="button" class="btn btn-primary" value="Close" data- dismiss="modal" />
</div>
</div>
</form>
<script type="text/javascript">
var onComplete = function () {
};
var onSuccess = function (context) {
};
var onFailed = function (context) {
};
</script>
控制器
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<JsonResult> EditMultipleEmployee(EmployeeModel empModel)
{
//CODE HERE HOW TO HANDLE LIST BOX VALUES FOR EACH EMP
}
模型
public class EmployeeModel
{
public string[] Reimbursement{get;set;}
public string[] EmployeeID{get;set;}
}
public class NewWOModel
{
public List<MultipleEmployeeModels> MultipleEmployeeModels{get; set;}
public List<ReimbursementModel> ReimbursementModels{ get; set; }
}
public class MultipleEmployeeModels
{
public string EmployeeNo{get;set;}
public int EmployeeID{get;set;}
}
public class ReimbursementModel
{
public int ReimbursementID{get;set;}
public string ReimbursementName{get;set;}
}
答案 0 :(得分:1)
由于您在Employee和Reimbursement之间存在关系,因此您需要更改模型以反映该关系(您拥有的只有2个独立的集合)。您需要以下视图模型
std::span
并在GET方法中传递public class EmployeeVM
{
public int EmployeeID { get; set; }
.... // other properties of Employee that you want in the view
public int[] SelectedReimbursements { get; set; } // the selected reimbursements for an employee
}
public class EmployeeCollectionVM
{
public List<EmployeeVM> Employees { get; set; }
public IEnumerable<ReimbursementModel> ReimbursementOptions { get; set; }
}
的实例到视图,这将是
EmployeeCollectionVM
将发回
@model EmployeeCollectionVM
<form id="frmWODetail" asp-action="EditMultipleEmployee" ....... >
....
@for(int i = 0; i < Model.Employees.Count; i++)
{
<input type="hidden" asp-for="Employees[i].EmployeeID />
<select asp-for="Employees[i].SelectedReimbursements" asp-items="@(new SelectList(Model.ReimbursementOptions,"ReimbursementID","ReimbursementName"))" multiple></select>
....
</form>