如何从View向Controller提交多条SubPeriods数据
批量提交多个数据对象至Controller的问题
我需要将多个SubPeriods数据对象提交到Controller,数据库中Date、Amount、Type字段目前为空,需用户填写这些字段(用户可选择任意项填写),点击保存按钮后,程序要将修改提交并更新到数据库。
页面说明
页面展示一个表格,包含三列:Date、Amount、Per Diem Type。每行对应一条待处理的数据:
- Date列是可输入的日期框
- Amount列是禁用的输入框,通过选择Per Diem Type的下拉选项触发JS自动填充值
- Per Diem Type列是下拉选择框,包含Transportation、Accomodation、VPN Fee三个选项
Razor视图代码
<table class="table table-bordered table-striped"> <thead> <tr> <th> Date </th> <th> Amount </th> <th> Per Diem Type </th> </tr> </thead> <tbody > @foreach (var item in Model) { <tr> <td> <input asp-for="@item.Date" class="form-control" autocomplete="off" /> </td> <td> <input asp-for="@item.Amount" class="form-control" id="amount-@item.Id" disabled> </td> <td> <label for="cars">Choose a car:</label> <select name="cars" id="pardiem-type-@item.Id" onclick="fillAmountValue('amount-@item.Id','pardiem-type-@item.Id')"> <option value="Transportation">Transportation</option> <option value="Accomodation">Accomodation</option> <option value="VPN Fee">VPN Fee</option> </select> </td> </tr> } </tbody> </table> <button type="submit" class="btn btn-primary">Save</button>
模型代码
仅Date、Amount和Type字段需要用户输入:
public class SubPeriods { public int Id { get; set; } public DateTime? Date { get; set; } public string EmployeeId { get; set; } [ForeignKey("EmployeeId")] [ValidateNever] public EmployeeIdentity EmployeeIdentity { get; set; } public int? Amount { get; set; } public string? Type { get; set; } }
内容的提问来源于stack exchange,提问作者omneer
相关产品推荐
相关产品推荐

