2013-07-01 24 views
0

我已经创建了一个表格,用于为每一行复选一个复选框。 所述显示是由这种方式:MVC4和剃刀:如何恢复选中的框?

@{ 
    ViewBag.Title = "NewInventory"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h3>Select rooms for new inventory</h3> 
@Html.Partial("RoomListInventory") 
<h3>Select people to assign to</h3> 
@Html.Partial("PersonListInventory") 

在“主”视图和局部视图是,即:

<table class="table table-striped"> 
    <thead> 
     <tr> 
      <th> 
       Select 
      </th> 
      <th> 
       User name 
      </th> 
      <th> 
       User role 
      </th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td> 
       <input type="checkbox" value="1"> 
      </td> 
      <td> 
       User 1 
      </td> 
      <td> 
       Role 1 
      </td> 
     </tr> 
     [...] 
    </tbody> 
</table> 

我的问题是:如何恢复然后,由于该值复选框将是该人的身份,还是对象或无论如何,如何恢复已选择框?

谢谢!

回答

0

您可以使用JQuery通过所有复选框搜索:

var list = $('input[type="checkbox"]').is(':checked'); 
var length = list.length; 
for(int i = 0; i < length; i++) 
{ 
    console.log(list[i].val()); 
} 

注意:您没有提供一个名称的复选框,这样在表单提交时,它可能不会延续到服务器。为其分配一个值以确保它可以与表单一起提交。

<input type="checkbox" value="1" name="user">