将HTML表格发布到ADO.NET DataTable

我的视图中有一个HTML表格,如下所示:

<table id="tblCurrentYear"> <tr> <td>Leave Type</td> <td>Leave Taken</td> <td>Leave Balance</td> <td>Leave Total</td> </tr> @foreach (var item in Model.LeaveDetailsList) { <tr> <td>@Html.TextBoxFor(m => item.LeaveType, new { width = "100" })</td> <td>@Html.TextBoxFor(m => item.LeaveTaken, new { width = "100" })</td> <td>@Html.TextBoxFor(m => item.LeaveBalance, new { width = "100" })</td> <td>@Html.TextBoxFor(m => item.LeaveTotal, new { width = "100" })</td> </tr> } </table> 

我想迭代所有的html表行,并在ADO.NET DataTable中插入值。

简单来说,将HTML Table转换为ADO.NET DataTable。

如何从HTML表中提取值并插入ADO.NET DataTable?

该视图基于以下模型

 public class LeaveBalanceViewModel { public LeaveBalanceViewModel() { this.EmployeeDetail = new EmployeeDetails(); this.LeaveBalanceDetail = new LeaveBalanceDetails(); this.LeaveDetailsList = new List<LeaveBalanceDetails>(); } public EmployeeDetails EmployeeDetail { get; set; } public LeaveBalanceDetails LeaveBalanceDetail { get; set; } public List<LeaveBalanceDetails> LeaveDetailsList { get; set; } } 

为了在回发后绑定到模型,表单控件的name属性必须与模型属性相匹配。 您使用foreach循环不会生成正确的名称属性。 如果你检查的HTML你会看到多个实例

 <input type="text" name="item.LeaveType" .../> 

但为了绑定到你的模型,控件将需要

 <input type="text" name="LeaveDetailsList[0].LeaveType" .../> <input type="text" name="LeaveDetailsList[1].LeaveType" .../> 

最简单的思考方法是考虑如何访问C#代码中的LeaveType属性的值

 var model = new LeaveBalanceViewModel(); // add some LeaveBalanceDetails instances to the LeaveDetailsList property, then access a value var leaveType = model.LeaveDetailsList[0].LeaveType; 

由于您的POST方法将有一个参数名称(比如model ),只需删除前缀( model ),这就是控件的名称属性必须如何。 为了做到这一点,你必须使用一个for循环(集合必须实现IList<T>

 for(int i = 0; i < Model.LeaveDetailsList.Count; i++) { @Html.TextBoxFor(m => m.LeaveDetailsList[i].LeaveType) .... } 

或者使用一个自定义的EditorTemplate (集合只需要实现IEnumerable<T>

/Views/Shared/EditorTemplates/LeaveBalanceDetails.cshtml

 @model yourAssembly.LeaveBalanceDetails <tr> <td>@Html.TextBoxFor(m => m.LeaveType)</td> .... </tr> 

然后在主视图中(不是在循环中)

 <table> .... // add headings (preferably in a thead element <tbody> @Html.EditorFor(m => m.LeaveDetailsList) </tbody> </table> 

最后,在控制器中

 public ActionResult Edit(LeaveBalanceViewModel model) { // iterate over model.LeaveDetailsList and save the items } 

关于你的要求,试试这个

 jQuery(document).on("change", ".DDLChoices", function (e) { var comma_ChoiceIds = ''; var comma_ChoicesText = ''; $('input[class="DDLChoices"]').each(function (e) { if (this.checked) { comma_ChoiceIds = comma_ChoiceIds + $(this).val() + ','; comma_ChoicesText = comma_ChoicesText + $(this).parent('label').parent() + ','; } }); $('#ChoiceIds').val(comma_ChoiceIds); $('#ChoiceText').val(comma_ChoicesText); }); 
 @using (Html.BeginForm("Actionname", "Controllername", FormMethod.Post, new { id = "frmChoices" })) { @Html.HiddenFor(m => m.ChoiceText, new { @id = "ChoiceText" }) @Html.HiddenFor(m => m.ChoiceIds, new { @id = "ChoiceIds" }) <div class="form-group"> <div> <table> <tr> <th>Name</th> <th>Selected</th> </tr> @foreach (var item in @Model.Choices) { <tr> <td> <label>@item.ChoicesText</label> </td> <td> <input class="DDLChoices" value="@item.ChoiceIds" type="checkbox" /></td> </tr> } </table> </div> <input type="button" value="Submit" onclick="return ChoicesPoster.passChoices()" </div> }