RKh*_*RKh 61 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>
@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>
Run Code Online (Sandbox Code Playgroud)
我想遍历所有html表行并在ADO.NET DataTable中插入值.
简单来说,将HTML表转换为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; }
}
Run Code Online (Sandbox Code Playgroud)
小智 70
为了在回发时绑定到模型,name
表单控件的属性必须与模型属性匹配.使用foreach
循环不会生成正确的名称属性.如果您检查html,您将看到多个实例
<input type="text" name="item.LeaveType" .../>
Run Code Online (Sandbox Code Playgroud)
但为了绑定到您的模型,控件需要
<input type="text" name="LeaveDetailsList[0].LeaveType" .../>
<input type="text" name="LeaveDetailsList[1].LeaveType" .../>
Run Code Online (Sandbox Code Playgroud)
考虑这个问题最简单的方法是考虑如何LeaveType
在C#
代码中访问属性的值
var model = new LeaveBalanceViewModel();
// add some LeaveBalanceDetails instances to the LeaveDetailsList property, then access a value
var leaveType = model.LeaveDetailsList[0].LeaveType;
Run Code Online (Sandbox Code Playgroud)
由于您的POST方法将具有参数名称(例如model
),只需删除前缀(model
),这就是控件的name属性必须如何.为了做到这一点,你必须使用一个for
循环(集合必须实现IList<T>
)
for(int i = 0; i < Model.LeaveDetailsList.Count; i++)
{
@Html.TextBoxFor(m => m.LeaveDetailsList[i].LeaveType)
....
}
Run Code Online (Sandbox Code Playgroud)
或使用自定义EditorTemplate
(集合只需要实现IEnumerable<T>
)
在 /Views/Shared/EditorTemplates/LeaveBalanceDetails.cshtml
@model yourAssembly.LeaveBalanceDetails
<tr>
<td>@Html.TextBoxFor(m => m.LeaveType)</td>
....
</tr>
Run Code Online (Sandbox Code Playgroud)
然后在主视图中(不在循环中)
<table>
.... // add headings (preferably in a thead element
<tbody>
@Html.EditorFor(m => m.LeaveDetailsList)
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
最后,在控制器中
public ActionResult Edit(LeaveBalanceViewModel model)
{
// iterate over model.LeaveDetailsList and save the items
}
Run Code Online (Sandbox Code Playgroud)
关于您的要求,试试这个
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);
});
Run Code Online (Sandbox Code Playgroud)
@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>
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8747 次 |
最近记录: |