zoukankan      html  css  js  c++  java
  • 在 mvc 中使用下拉列表

    在mvc中经常会使用到下拉列表,以下以两种方式来实现,一种是以  @Html.DropDownList 扩展方法,一种是以 <select><option></option></select> 这样的 html 代码和 HashTable来实现

    1.  @Html.DropDownList 扩展方法

     View 代码:

                @if (ViewData["listCategory"] != null)
                {
                    @Html.DropDownList("listcategory", ViewData["listCategory"] as IEnumerable<SelectListItem>, "---请选择类型---")
                }
    View Code

    Control 代码:

     ViewData["listCategory"] = cardCategory.GetAll();
    
      public List<Sns.Management.Model.CardCategory> GetAll()
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select ");
                strSql.Append(" id,category_name");
                strSql.Append(" from pro_card_category");
                strSql.Append(" order by sort asc");
                DataSet ds = DbHelperMySQL.Query(strSql.ToString());
                System.Collections.Hashtable hashtable = new System.Collections.Hashtable();
    
                List<Sns.Management.Model.CardCategory> mylist = new List<Model.CardCategory>();
    
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        Sns.Management.Model.CardCategory model = new Model.CardCategory();
                        model.Id = row[0].ToString();
                        model.CategoryName = row[1].ToString();
                        mylist.Add(model);
                    }
                }
                return mylist;
            }
    View Code

    2. 使用 html 标签 + HashTable

     View 代码:

                <select style="130px;" id="CategoryId" name="CategoryId">
                    <option value="0">请选择分类名称</option>
                    @if (ViewData["listCategory"] != null)
                    {
                        System.Collections.Hashtable myhashtable = ViewData["listCategory"] as System.Collections.Hashtable;
    
                        foreach (System.Collections.DictionaryEntry item in myhashtable)
                        {
                            if (Model != null && Model.CategoryId == item.Key.ToString())
                            {
                                <option value="@item.Key" selected="selected">@item.Value</option>
    
                            }
                            else
                            {
                                <option value="@item.Key">@item.Value</option>
                            }
                        }
                    }
    
                </select>
    
                <select style="130px;" id="CategoryId" name="CategoryId">
                    <option value="0">请选择分类名称</option>
                    @if (ViewData["listCategory"] != null)
                    {
                        System.Collections.Hashtable myhashtable = ViewData["listCategory"] as System.Collections.Hashtable;
    
                        foreach (System.Collections.DictionaryEntry item in myhashtable)
                        {
                            <option value="@item.Key">@item.Value</option>
                        }
                    }
    
                </select>
    View Code

    Control 代码:

     ViewData["listCategory"] = skinCategory.GetAll();
    
      /// <summary>
            ///  hashtable  key: id的值,  value: 分类名称
            /// </summary>
            /// <returns></returns>
            public System.Collections.Hashtable GetAll()
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select ");
                strSql.Append(" id,category_name");
                strSql.Append(" from pro_skin_category");
                strSql.Append(" order by sort asc");
                DataSet ds = DbHelperMySQL.Query(strSql.ToString());
                System.Collections.Hashtable hashtable = new System.Collections.Hashtable();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        hashtable.Add(row[0].ToString(), row[1].ToString());
                    }
                }
                return hashtable;
    
            }
    View Code
  • 相关阅读:
    sitemap怎么制作才适合蜘蛛抓取?
    网站高并发优化性能调优总结
    圆柱模板行业B2B站点打造MIP推送+熊掌号推送+历史普通推送插件
    a href="javascript:void(0)" 是什么意思?加不加上有什么区别?
    <a href="#" onclick="history.back();"></a>这样写为什么是对的? -(转)
    form 表单 enctype 属性-(转自w3c)
    关于submit与document.form1.submit();这2个提交的区别
    zf-表单填写以及相关业务流程
    zf-关于把某个地址的svn项目移动到另一个上面的步骤
    zf-关于即将过期提示字符串的修改
  • 原文地址:https://www.cnblogs.com/wisdo/p/4357621.html
Copyright © 2011-2022 走看看