处理asp.net核心表单中的多选列表框值,并提交多条记录

莎玛

在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>
        </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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章