发布时间:2025-12-10 19:20:46 浏览次数:5
@Html.DropDownList()的四种用法及自定义DropDownList扩展常用方法后台代码:publicActionResultIndex(){ViewData[“deptOu”]=”SOHO”;using(ISessionsession=newNHibernateHelper(DataBase.ADDB).OpenSession()){IList<t_data_DeptOU>dep…
常用方法后台代码:
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();} 是否还在为Ide开发工具频繁失效而烦恼,来吧关注以下公众号获取最新激活方式。亲测可用!
【正版授权,激活自己账号】:Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】:官方授权 正版激活 自己使用,支持Jetbrains家族下所有IDE…
常用方法前台代码:
<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 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.Htmlnamespace 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”,无法禁止选择