【发布时间】:2015-05-26 10:39:44
【问题描述】:
视图如下:
@using (Html.BeginForm("AddCreditLeave", "AdminLeaveCredit"))
{
<div id="Container">
<table>
<tr style="background-color:#A9DDFF;color:Black;">
<td>Select</td>
<td>Staff Code</td>
<td>Name</td>
<td>Designation</td>
<td>AL</td>
<td>CL</td>
</tr>
@foreach (var person in Model.Empdetailslist)
{
<tr>
<td>
<input type="checkbox" name="sid" value="@person.staffcode" id="chk" />
</td>
<td>@Html.TextBoxFor(m => person.staffcode, new { @class = "ReadOnly", @readonly = "readonly", style = "width:180px; text-align:center" })</td>
<td>@Html.TextBoxFor(m => person.name, new { @class = "ReadOnly", @readonly = "readonly", style = "width:180px; text-align:center" })</td>
<td>@Html.TextBoxFor(m => person.designation, new { @class = "ReadOnly", @readonly = "readonly", style = "width:180px; text-align:center" })</td>
<td>@Html.TextBoxFor(m => person.ALLeave, new { style = "width:180px; text-align:center" })</td>
<td>@Html.TextBoxFor(m => person.CLLeave, new { style = "width:180px; text-align:center" })</td>
</tr>
}
</table>
}
第一列是一个复选框,我显示十行。
第二列是一个文本框。
有一个保存按钮。单击保存按钮时,我想从选中复选框的表单集合中选择第二列的值。
如何做到这一点?
【问题讨论】:
-
答案可以用jquery吗?
-
未选中的复选框不会回发,因此回发
sid将包含原始staffcode值的数组。但是,您的代码没有多大意义,因为您有一个staffcode的文本框,但是因为您使用foreach循环而不是必需的for循环,所以它无论如何都不起作用(并且您有很多无效的 html,因为重复的id属性) -
@StephenMuecke 哦。你的意思是我应该使用 For 循环而不是 foreach 并且名称应该像 staffcode[i] ?
-
您需要的是一个包含(比如说)
bool IsSelected属性的视图模型。然后在视图中使用for循环 -for ( int i = 0; i < Model.Count; i++) { @Html.CheckBoxFor(m => m.IsSelected) ... }并回发您的视图模型集合(忘记使用FormCollection) -
@StephenMuecke 虽然我有一些想法,但如果您详细说明并作为答案发布,这将非常有帮助。
标签: c# asp.net asp.net-mvc razor asp.net-mvc-5