C# 将HTML表发布到ADO.NET DataTable
我的视图中有一个HTML表,如下所示:C# 将HTML表发布到ADO.NET DataTable,c#,asp.net-mvc,razor,asp.net-mvc-5,C#,Asp.net Mvc,Razor,Asp.net Mvc 5,我的视图中有一个HTML表,如下所示: <table id="tblCurrentYear"> <tr> <td>Leave Type</td> <td>Leave Taken</td> <td>Leave Balance</td> <td>Leave Total</td> </tr>
<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>
休假类型
请假
剩余余额
总假期
@foreach(Model.LeaveDetailsList中的var项)
{
@Html.TextBoxFor(m=>item.LeaveType,新的{width=“100”})
@Html.TextBoxFor(m=>item.leavetake,新的{width=“100”})
@Html.TextBoxFor(m=>item.LeaveBalance,新的{width=“100”})
@Html.TextBoxFor(m=>item.LeaveTotal,新的{width=“100”})
}
我希望遍历所有html表行,并在ADO.NET DataTable中插入值
简单地说,将HTML表转换为ADO.NET数据表
如何从HTML表中提取值并插入ADO.NET数据表
该视图基于以下模型
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; }
}
公共类LeaveBalanceViewModel
{
公共假期余额视图模型()
{
this.EmployeeDetail=新的EmployeeDetails();
this.LeaveBalanceDetail=新的LeaveBalanceDetails();
this.LeaveDetailsList=新列表();
}
公共EmployeeDetail EmployeeDetail{get;set;}
public LeaveBalanceDetails LeaveBalanceDetail{get;set;}
公共列表LeaveDetailsList{get;set;}
}
为了绑定回发时的模型,表单控件的名称
属性必须与模型属性匹配。使用foreach
循环不会生成正确的名称属性。如果您检查html,您将看到
<input type="text" name="item.LeaveType" .../>
因为POST方法将有一个参数名(比如model
),所以只需删除前缀(model
),控件的name属性就必须是这样的。为此,必须使用for
循环(集合必须实现IList
)
然后在主视图中(不在循环中)
为了在回发时绑定到模型,表单控件的
name
属性必须与模型属性匹配。使用foreach
循环不会生成正确的名称属性。如果您检查html,您将看到
<input type="text" name="item.LeaveType" .../>
因为POST方法将有一个参数名(比如model
),所以只需删除前缀(model
),控件的name属性就必须是这样的。为此,必须使用for
循环(集合必须实现IList
)
然后在主视图中(不在循环中)
根据你的要求,试试这个
jQuery(document).on(“change”和“.ddlschoices”,函数(e){
var comma_choiceId='';
var comma_ChoicesText='';
$('input[class=“DDLChoices”]”)。每个(函数(e){
如果(选中此项){
逗号_choiceId=逗号_choiceId+$(this.val()+',';
逗号\选项文本=逗号\选项文本+$(this).parent('label').parent()+',';
}
});
$('#choiceId').val(逗号#choiceId);
$('#ChoiceText').val(逗号#ChoiceText);
});
@使用(Html.BeginForm(“Actionname”、“Controllername”、FormMethod.Post、new{id=“frmChoices”}))
{
@Html.HiddenFor(m=>m.ChoiceText,新的{@id=“ChoiceText”})
@HiddenFor(m=>m.choiceId,新的{@id=“choiceId”})
名称
挑选出来的
@foreach(@Model.Choices中的var项)
{
@item.ChoicesText
}
根据你的要求,试试这个
jQuery(document).on(“change”和“.ddlschoices”,函数(e){
var comma_choiceId='';
var comma_ChoicesText='';
$('input[class=“DDLChoices”]”)。每个(函数(e){
如果(选中此项){
逗号_choiceId=逗号_choiceId+$(this.val()+',';
逗号\选项文本=逗号\选项文本+$(this).parent('label').parent()+',';
}
});
$('#choiceId').val(逗号#choiceId);
$('#ChoiceText').val(逗号#ChoiceText);
});
@使用(Html.BeginForm(“Actionname”、“Controllername”、FormMethod.Post、new{id=“frmChoices”}))
{
@Html.HiddenFor(m=>m.ChoiceText,新的{@id=“ChoiceText”})
@HiddenFor(m=>m.choiceId,新的{@id=“choiceId”})
名称
挑选出来的
@foreach(@Model.Choices中的var项)
{
@item.ChoicesText
}
您正在寻找获取表的文本框值并插入到数据库中,还是需要插入完整的html?从我们对上一个问题的评论中是否不清楚,您不能使用foreach
循环来生成集合中的控件。您需要for
循环或自定义EditorTemplate
模型。您的foreach
loop不会绑定到任何东西。@StephenMuecke但foreach正在工作,我可以用它填充HTML表。生成此表后,用户可以更改任何文本框中的值,然后单击“保存”按钮。单击“保存”后,我想选择所有文本框值(逐行)并插入到ADO.NET DataTable中。不,它不是-您可以在视图中看到值,但回发时无法绑定任何内容。检查生成的html-您有多个带有name=“LeaveType”
的文本框。要绑定回发时的集合,控件必须是LeaveBalanceDetail[0].LeaveType
,LeaveBalanceDetail[1].LeaveType
等。为什么需要将这些添加到ADO.Net表中?是否希望将其保存到数据库中?是否希望获取表的文本框值并插入
var model = new LeaveBalanceViewModel();
// add some LeaveBalanceDetails instances to the LeaveDetailsList property, then access a value
var leaveType = model.LeaveDetailsList[0].LeaveType;
for(int i = 0; i < Model.LeaveDetailsList.Count; i++)
{
@Html.TextBoxFor(m => m.LeaveDetailsList[i].LeaveType)
....
}
@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
}