利用DropDownList实现下拉

在视图的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

转载于:https://www.cnblogs.com/netxiaohui/p/4896262.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值