我想獲取選中複選框值的列表。我該怎麼做?我正在開發一個ASP.NET MVC 5應用程序。這個程序有一個基本的形式。 獲取asp.net中選中複選框值的列表mvc
我的視圖代碼
@using (Html.BeginForm())
{
<table class="table">
<thead>
<tr>
<th>Field Name</th>
@foreach (var field in Model.Actions)
{
<th>@field.Name</th>
}
</tr>
</thead>
<tbody>
@foreach (var permission in Model.PermissionUserField)
{
<tr>
<td> @permission.Field.Name</td>
@foreach (var peruserAction in permission.PermissionUserFieldActions)
{
<td>
@if (peruserAction.Active)
{
@Html.HiddenFor(x=> x.PermissionUserFieldForUser)
<input type="checkbox" checked name="[email protected](permission.FieldId)_[@peruserAction.Id]" />
}
else
{
<input type="checkbox" name="[email protected](permission.FieldId)_[@peruserAction.Id]" />
}
</td>
}
</tr>
}
</tbody>
</table>
<br />
<input type="submit" value="Save" />
}
我的表單模型如下所示:
public class NewViewModel
{
public List<PermissionUserField> PermissionUserField{ get; set; }
public List<PermissionUserField> PermissionUserFieldForUser { get; set; }
public List<Action> Actions { get; set; }
public List<Field> Field { get; set; }
}
public partial class Action
{
public int Id { get; set; }
[StringLength(50)]
public string Name { get; set; }
}
public partial class PermissionUserField
{
public int Id { get; set; }
public int UserId { get; set; }
public User User { get; set; }
public int FieldId { get; set; }
public Field Field { get; set; }
public List<PermissionUserFieldAction> PermissionUserFieldActions { get; set; }
}
public partial class PermissionUserFieldAction
{
public int Id { get; set; }
public int ActionId { get; set; }
public Action Action { get; set; }
public int PermissionUserFieldId { get; set; }
public PermissionUserField PermissionUserField { get; set; }
public bool Active { get; set; } = false;
}
我的控制器代碼:
嗯......你似乎已經走出留下控制器代碼控制器? –
是否有你沒有使用Html.CheckBoxFor的原因? – Neil
你不給你的複選框一個「價值」。但是你不能使用'foreach'循環爲集合生成表單控件 - 參考[這個答案](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943 )也不能將隱藏的輸入綁定到複雜對象。 –