MVC 绑定下拉列表

来源:互联网 发布:知与行投稿 编辑:程序博客网 时间:2024/05/21 00:52

        //        // 摘要:        //     使用列表的指定项、数据值字段、数据文本字段和选定的值来初始化 System.Web.Mvc.SelectList 类的新实例。        //        // 参数:        //   items:        //     各个项。        //        //   dataValueField:        //     数据值字段。        //        //   dataTextField:        //     数据文本字段。        //        //   selectedValue:        //     选定的值。        public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue);



方法一:

1、视图

      @Html.DropDownList("examplanid")

2、控制器

      ViewBag.examplanid=new SelectList(ExamPlanList.GetExamPlanList(pid),"ExamPlanId","ExamineeId",Pid);

                                                                                         数据源                                             值                    文本        默认值

 

 

方法二:

1、视图

      @Html.DropDownList("Nation",String.Empty)

2、控制器 

      ViewData.Add("Nation", new SelectList(mylist.Where(o => o.SelectID == 1), "NormalSelectItem", "NormalSelectItem", eop.Nation));   

 

方法三:

1、视图

     @Html.DropDownList("lb",string.Empty)         //string.Empty 加一个空项目

2、控制器       

         List<SelectListItem> lists = new List<SelectListItem>
            {
                new SelectListItem{Text="大学",Value="大学"},
                new SelectListItem{Text="高中",Value="高中"},
                new SelectListItem{Text="初中",Value="初中"}
            };
    ViewBag.lb=new SelectList(list,"Value","Text",默认值)


方法四、

1、控制器

     

ViewBag.examplanid = new SelectList(ExamPlanList.GetExamPlanList2("职业资格"), "ExamPlanId", "ExamBigItem", pid);//绑定下拉列表(考试类别)


2、视图

            <select id="Select1">                @foreach (var examplan in ViewBag.examplanid)                {                <option value="@examplan.Value">@examplan.Text</option>                }            </select>



 

 

原创粉丝点击