如何数据绑定DropDownlist和Checkbox?

时间:2012-07-18 07:34:44

标签: asp.net-mvc-3 c#-4.0 sql-server-2008-r2

我有一个数据表,我已经添加了分配工作链接....当点击它时,它带我到一个assinedwork按钮,其中我dropdownlist绑定projectName中的数据...和像复选框...

Table for Projects
-----------

ProjetsId  |  ProjectName  |Decscription..
 1         |    aaaa       |    hihihihi
 2         |   bbbb         |  helohelo

 like that .....
 to bind the project name to Dropdownlist.

Table for Employee
-------------------
EmployeeId  |  EmployeNameName  |EmployeDescription..
 1         |    aaaa            |    hihihihi
 2         |   bbbb             |  helohelo

to bind the EmployeName to Checkbox
please help me .....................

1 个答案:

答案 0 :(得分:0)

绑定到DropDownList

基本上,您必须创建一个可枚举的SelectListItem集合,并通过 ViewBag 查看模型将其传递给控制器​​。

从行动中,

public ViewResult Edit(int id)
{
   ...

   var projects = _repository.GetAllProjects().Select(p => 
                     new SelectListItem
                     {
                        Text = p.ProjectName,
                        Value = p.ProjectsId.ToString()
                     });

   ViewBag.Projects = projects; // you can also use viewmodel

   ...
}

从视图来看,

@Html.DropDownListFor(x => x.ProjectId, ViewBag.Projects, "Choose a project")