·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> 利用DropDownList实现下拉

利用DropDownList实现下拉

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

在视图的Model<Vo>里面我们需要使用IEnumerable来将别的列表的数据全部的转化为下拉列表。下面是关于在项目中实际的写法。

一:实现下拉属性列表的写法

  通过使用SelectListItem来自动填充到DropDownList中,形成下拉框。

  我们想要在前台页面将数据变为下拉就要用到DropDownList这个特性标签来实现,但是使用的前提是要在Action里面进行设置,对Value和Text进行赋值。

下面是属性的写法,是IEnumerable<>接口类型

    public class CreatCustomerView
    {
        public CreatCustomerView()
        {
            this.Schools = new List<SelectListItem>();
        }
        /// <summary>
        /// 外键
        /// </summary>
        [Display(Name = "学校"), Required(ErrorMessage = "不能选择为空")]
        public Guid SchoolId { get; set; }
        /// <summary>
        /// 学校的导航属性
        /// </summary>
        public IEnumerable<SelectListItem> Schools { get; set; }
        /// <summary>
        /// OpenId:跟微信绑定的唯一表示,从微信处获取
        /// </summary>
        public string OpenId { get; set; }
    }

写成这样就是想将其Schools放在一个集合里面,而且在上面初始化的时候写成了SelectListItem。

   SelectListItem代表System.Web.Mvc的实例的选择项。SelectList类。这里将在Action里面进行相关的设置。

IEnumerable<T>接口类型:这个是实现Foreach遍历所必须的,因为所有的集合和数据都继承自这个接口,并且支持非泛型方法的简单迭代,是集合访问器。定义一种扩展方法,用来对数据集合中元素进行遍历,过滤,排序,搜索等操作。

二:在Action里面的写法 

这里就是为其Value和Text进行赋值。

        public ActionResult ChooseSchool()
        {
             var entity = new CreatCustomerView();
             entity.Schools = _schoolService.GetAll()
                     .Where(x => x.Id != Guid.Empty)
                     .Select(x => new SelectListItem
                      {
                          Text = x.Name,
                          Value = x.Id.ToString()
                       }).ToList();
               return View(entity);
        }

首先通过GetALL方法来取出数据库表中的数据,通过Select对其进行赋值,转换为ToList()的形式,在将其传到视图。这里就是为其里面赋值,为将来在前台页面进行Foreach做准备。

三:View视图里面的写法

在视图里面是通过HtmlHelper中的DropDownList来实现的,但是DropDownList的现实要通过下面的三个步奏来实现。 

image

其实就是前面两个步奏中的内容,下面是View中的代码。

@{
    ViewBag.Title = "选择学校";
    Layout = "~/Views/Shared/_LayoutNew.cshtml";
}
@using System.Runtime.InteropServices
@model ExPRess.Weixin.Web.Models.CreatCustomerView
<div class="header"><a href="@Url.Action("Index","Member")" class="btn btn-link btn-xs pull-left">返回</a>选择学校</div>
@using (Html.BeginForm(null, null, FormMethod.Post))
{
    <input type="hidden" value="@ViewBag.OpenId" name="OpenId" />
    <div class="col-sm-5 center" style="margin: auto;position: absolute; top: 0; left: 0; bottom: 0; right: 0; ">
        <br/><br/><br />
        @Html.LabelFor(x => x.SchoolId)
        @Html.DropDownListFor(x => x.SchoolId, Model.Schools, new { @class = "form-control" })
        @Html.ValidationMessageFor(x => x.SchoolId)
        <br/>
        <input type="submit" class="btn btn-success btn-sm btn-block" value="选择学校"/>
    </div>
}

通过里面的@Html.DropDownListFor(x => x.SchoolId, Model.Schools, new { @class = "form-control" }) 来实现下拉的结果。

四:显示结果

I%`01{V`U96T36@V_U5GIFL

附件:DropDownList知识参考资料 http://www.cnblogs.com/kirinboy/archive/2009/10/28/use-dropdownlist-in-asp.net-mvc.html