【问题标题】:CheckBox List in ASP.net MVC3ASP.net MVC3 中的复选框列表
【发布时间】:2014-03-19 14:02:37
【问题描述】:

我有一个主管表格,我需要显示一个带有复选框的员工列表以供选择。

单击 Save 后,主管信息和选定的员工 ID 应保存到 DB。

实现这一目标的最佳选择是什么?

【问题讨论】:

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


【解决方案1】:

型号

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);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2012-11-05
    相关资源
    最近更新 更多