gpt4 book ai didi

jquery - 处理带有多条记录的 asp.net core 表单提交中的多选列表框值

转载 作者:行者123 更新时间:2023-12-01 04:36:55 25 4
gpt4 key购买 nike

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

Controller

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

最佳答案

由于您在员工和报销之间存在关系,因此您需要更改模型以反射(reflect)该关系(您拥有的只是 2 个独立的集合)。您需要以下 View 模型

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 的实例传递给 View ,这将是

@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)

关于jquery - 处理带有多条记录的 asp.net core 表单提交中的多选列表框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50113462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com