常用方法后台代码:
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”,无法禁止选择
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/183554.html原文链接:https://javaforall.cn