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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaouncle/article/details/82856982

常用方法后台代码:

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>


猜你喜欢

转载自blog.csdn.net/xiaouncle/article/details/82856982