有一个 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中?
该view基于以下模型
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 循环不会生成正确的 name 属性。如果你检查 html,会看到这个
但是为了绑定到你的模型,control 要
反正就是这样子之类的。考虑这个问题的最简单方法是考虑如何在 c # 代码中访问 LeaveType 属性的值
var model = new LeaveBalanceViewModel();// add some LeaveBalanceDetails instances to the LeaveDetailsList property, then access a valuevar leaveType = model.LeaveDetailsList[0].LeaveType;
因为您的 POST 将有一个参数名(比如model) ,所以只需删除前缀(model) ,这就是控件的 name 属性必须的样子。 为此,必须使用forloop (集合必应用IList)
for(int i = 0; i < Model.LeaveDetailsList.Count; i++){
@Html.TextBoxFor(m => m.LeaveDetailsList[i].LeaveType)
....}
或使用自定义 EditorTemplate (集合只需实现 IEnumerable)
在/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>
}