ASP.net MVC3中的CheckBox列表

时间:2014-03-19 04:41:10

标签: asp.net asp.net-mvc asp.net-mvc-3

我有一张SuperVisor表单,我需要显示一份员工列表,其中包含可供选择的复选框。

点击“保存”后,应将主管信息和所选员工ID保存到数据库。

哪个是实现这个目标的最佳选择?

1 个答案:

答案 0 :(得分:2)

<强>模型

public class SelectEmployee
{
    public int _EmployeeID;
    public string _EmployeeName;
    public bool _check;

    public int EmployeeID { get { return _EmployeeID; } set { _EmployeeID = value; } }
    public string EmployeeName { get { return _EmployeeName; } set { _EmployeeName = value; } }
    public bool check { get { return _check; } set { _check = value; } }
}
public class DemoManager
{
    public static List<SelectEmployee> GetSelectedEmployee()
    {
        DemoEntities db = new DemoEntities();
        var query = (from i in db.EmployeeMasters
                     select new SelectEmployee { EmployeeID = i.EmployeeID, EmployeeName = i.EmployeeName, check = false }).ToList();
        return query;
    }
}

查看

@using(Html.BeginForm()) 
{ 
<table class="table">

@for (var i = 0; i < Model.Count; i++)  {
    <tr>
        <td>
            @Html.DisplayFor(modelEmployee => modelEmployee[i].EmployeeID)
        </td>
        <td>
            @Html.CheckBoxFor(modelEmployee => modelEmployee[i].check)
        </td>
        <td>
            @Html.DisplayFor(modelEmployee => modelEmployee[i].EmployeeName)
        </td>
    </tr>
}       
</table>
    <input type="submit" value="click" />
}

控制器

    public ActionResult Index()
    {

        return View(DemoManager.GetSelectedEmployee());
    }

    [HttpPost]
    public ActionResult Index(List<SelectEmployee> emp)
    {
        var query = (from i in emp
                     where i.check == true
                     select i);
      // Here you can set the insert statements the query will contain only selected items

        return View(model);
    }