复选框列表在ASP.net MVC3 [英] CheckBox List in ASP.net MVC3

查看:113
本文介绍了复选框列表在ASP.net MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主管形式,我需要证明员工与复选框的选择列表。

I have an SuperVisor Form and I need to show a list of Employees with checkboxes for selection.

在我点击保存,主管信息和所选择的员工ID应该是保存到数据库中。

Once I click Save , supervisor information and the selected employees id should be saved to DB.

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

Which is best option to achieve this ?

推荐答案

型号

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

这篇关于复选框列表在ASP.net MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆