在ASP.NET CORE 2.0中,我正在创建一个Ajax表单。可以用此表格更新多个员工。有一个雇员编号和一个多选列表框,用于选择雇员的报销。
问题是如何处理偿还额,因为可以从列表框中选择多个项目。表单将作为键值对提交。如果“报销”,键为“报销”,且值将是多个。因此,如何检查为哪个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;}
    }

最佳答案

由于您在“雇员”与“报销”之间有关系,因此您需要更改模型以反映这种关系(您只有2个独立的集合)。您需要以下视图模型

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; }
}


然后在GET方法中,将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>


它将发回到

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<JsonResult> EditMultipleEmployee(EmployeeCollectionVM model)

07-24 09:44
查看更多