前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >html.dropdownlistfor_see的用法

html.dropdownlistfor_see的用法

作者头像
全栈程序员站长
发布2022-11-07 16:53:16
6930
发布2022-11-07 16:53:16
举报
文章被收录于专栏:全栈程序员必看

常用方法后台代码:

代码语言:javascript
复制
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();
}

常用方法前台代码:

代码语言:javascript
复制
<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>

运行截图如下:

html.dropdownlistfor_see的用法
html.dropdownlistfor_see的用法

自定义DropDownList扩展后台代码:

代码语言:javascript
复制
//实际开发中要把命名空间改为: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扩展前台代码:

代码语言:javascript
复制
@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”,无法禁止选择

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/183554.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档