@Html.DropDownList()的四种用法及自定义DropDownList扩展

常用方法后台代码:

public ActionResult Index()
{
    ViewData["deptOu"] = "SOHO";
    using (ISession session = new NHibernateHelper(DataBase.ADDB).OpenSession())
    {
        IList<t_data_DeptOU> deptOuList = session.QueryOver<t_data_DeptOU>().List();
        deptOuList.Insert(0, new t_data_DeptOU() 
        {
            OUName="-1",
            DeptName="---请选择---"
        });
        ViewData["deptOuList"] = deptOuList;
        ViewData["ddlDeptOu"] = new SelectList(deptOuList, "OUName", "DeptName");
    }
    return View();
}

常用方法前台代码:

<form method="post" action="/Home/Create">
    @Html.ValidationMessage("error")

    @*ddlDeptOu是id、name值,也是数据源的名称*@
    @Html.DropDownList("ddlDeptOu")

    @*deptOu是id、name值,ddlDeptOu是数据源的名称*@
    @Html.DropDownList("deptOu", (IEnumerable<SelectListItem>)ViewData["ddlDeptOu"])

    @*other是id、name值,ddlDeptOu是数据源的名称,当other不存在时默认选择第一项*@
    @Html.DropDownList("other", (IEnumerable<SelectListItem>)ViewData["ddlDeptOu"], new { style = "width:150px;height:23px;" })
    
    @*根据内容自己处理下拉列表*@
    <select id="deptOu" name="deptOu" style="width: 150px; height: 23px;">
        @foreach (t_data_DeptOU item in (IList<t_data_DeptOU>)ViewData["deptOuList"])
        {
            if (item.OUName == ViewData["deptOu"].ToString())
            {
                <option selected="selected" value="@item.OUName">@item.DeptName</option>
            }
            else
            {
                <option value="@item.OUName">@item.DeptName</option>
            }
        }
    </select>
    <input type="submit" value="提交" />
</form>

运行截图如下:

 自定义DropDownList扩展后台代码:

//实际开发中要把命名空间改为:System.Web.Mvc.Html
namespace MvcNHibernateFirst.Web.Extensions
{
    public static class SelectExtension
    {
        public static MvcHtmlString DDLDeptOu(this HtmlHelper htmlHelper, string name, object htmlAttributes)
        {
            return DDLDeptOu(htmlHelper, name, null, htmlAttributes);
        }

        public static MvcHtmlString DDLDeptOu(this HtmlHelper htmlHelper, string name, string selectedValue, object htmlAttributes)
        {
            using (ISession session = new NHibernateHelper(DataBase.ADDB).OpenSession())
            {
                IList<t_data_DeptOU> deptOuList = session.QueryOver<t_data_DeptOU>().List();
                deptOuList.Insert(0, new t_data_DeptOU()
                {
                    OUName = "-1",
                    DeptName = "---请选择---"
                });
                SelectList list = new SelectList(deptOuList, "OUName", "DeptName", selectedValue);
                return htmlHelper.DropDownList(name, list, htmlAttributes);
            }
        }
    }
}

 自定义DropDownList扩展前台代码:

@using MvcNHibernateFirst.Web.Extensions;
@{
    ViewBag.Title = "Index";
}

<form method="post" action="/Home/Create">
    <!--other是id、name值-->
    <!--ViewData["other"]不存在/值为null时,选中第一项-->
    <!--ViewData["other"]的值不属于列表项时,选中第一项-->
    <!--ViewData["other"]的值属于列表项时,选中value=ViewData["deptOu"]的项-->
    @Html.DDLDeptOu("other", new { style = "width:150px;height:23px" })
    
    <!--other是id、name值-->
    <!--ViewData["other"]不存在/值为null时,选中value="SOHO"的项-->
    <!--ViewData["other"]的值不属于列表项时,选中第一项-->
    <!--ViewData["other"]的值属于列表项时,选中value=ViewData["other"]的项-->
    @Html.DDLDeptOu("other", "SOHO", new { style = "width:150px;height:23px" })   
    
    <input type="submit" value="提交" />
</form>


下拉列表禁止选择且能获取到控件当前选择的值

disabled="true",有时无法获取当前选择的值

style="pointer-events:none",可以获取当前选择的值

readonly="readonly",无法禁止选择

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

changuncle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值