在MVC3中获取已检查复选框的ID

Sae*_*eid 5 asp.net-mvc formcollection razor asp.net-mvc-3

这是我的观点:

@foreach(var action in Model.Category.Actions) {
<div class="action" style="margin-right: 30px;">
    <input type="checkbox" class="chk-act" id="@action.Id" name="actionChk" />
    <text>@action.Text</text>
</div>
  }
Run Code Online (Sandbox Code Playgroud)

而html Dom就像以下几样:

<input type="checkbox" class="chk-act" id="17" name="actionChk">
<input type="checkbox" class="chk-act" id="18" name="actionChk">
<input type="checkbox" class="chk-act" id="19" name="actionChk">
Run Code Online (Sandbox Code Playgroud)

所以我需要检查ID.当我尝试通过表单集合获取值时,它返回了一个on按已检查复选框长度的字符串数组:

[HttpPost]
public ActionResult Index(FormCollection collection) {
    var actions = collection.GetValues("actionChk");
return View();
}
Run Code Online (Sandbox Code Playgroud)

你的建议是什么?

Iri*_*dio 9

您应该将值放在value参数中

 <input type="checkbox" class="chk-act" id="17" value="17" name="actionChk">
 <input type="checkbox" class="chk-act" id="18" value="18" name="actionChk">
 <input type="checkbox" class="chk-act" id="19" value="19" name="actionChk">
Run Code Online (Sandbox Code Playgroud)

然后,从控制器,您应该有一个Idsnamed 数组actionChk