在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>, "---请选择类型---")
}

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[].Rows.Count > )
{
foreach (DataRow row in ds.Tables[].Rows)
{
Sns.Management.Model.CardCategory model = new Model.CardCategory();
model.Id = row[].ToString();
model.CategoryName = row[].ToString();
mylist.Add(model);
}
}
return mylist;
}

2. 使用 html 标签 + HashTable

View 代码:

            <select style="width: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="width:130px;" id="CategoryId" name="CategoryId">
<option value="">请选择分类名称</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>

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[].Rows.Count > )
{
foreach (DataRow row in ds.Tables[].Rows)
{
hashtable.Add(row[].ToString(), row[].ToString());
}
}
return hashtable; }

在 mvc 中使用下拉列表的更多相关文章

  1. 基于MVC4+EasyUI的Web开发框架经验总结(6)--在页面中应用下拉列表的处理

    在很多Web界面中,我们都可以看到很多下拉列表的元素,有些是固定的,有些是动态的:有些是字典内容,有些是其他表里面的名称字段:有时候引用的是外键ID,有时候引用的是名称文本内容:正确快速使用下拉列表的 ...

  2. asp.net mvc中DropDownList

    asp.net mvc中DropDownList的使用. 下拉列表框 以分为两个部分组成:下拉列表和默认选项 DropDownList扩展方法的各个重载版本基本上都会传递到这个方法上:   publi ...

  3. .NetCore MVC中的路由(2)在路由中使用约束

    p { margin-bottom: 0.25cm; direction: ltr; color: #000000; line-height: 120%; orphans: 2; widows: 2 ...

  4. .NetCore MVC中的路由(1)路由配置基础

    .NetCore MVC中的路由(1)路由配置基础 0x00 路由在MVC中起到的作用 前段时间一直忙于别的事情,终于搞定了继续学习.NetCore.这次学习的主题是MVC中的路由.路由是所有MVC框 ...

  5. Asp.Net MVC中使用StreamReader读取“Post body”之应用场景。

    场景:有三个市场(Global.China.USA),对前台传过来的数据有些验证需要细化到每个市场去完成. 所以就出现了基类(Global)和派生类(China.USA) 定义基类(Global)Pe ...

  6. 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  7. ASP.NET Core MVC 中的 [Controller] 和 [NonController]

    前言 我们知道,在 MVC 应用程序中,有一部分约定的内容.其中关于 Controller 的约定是这样的. 每个 Controller 类的名字以 Controller 结尾,并且放置在 Contr ...

  8. ASP.NET MVC中利用AuthorizeAttribute实现访问身份是否合法以及Cookie过期问题的处理

    话说来到上海已经快半年了,时光如白驹过隙,稍微不注意,时间就溜走了,倒是没有那么忙碌,闲暇之际来博客园还是比较多的,记得上次在逛博问的时候看到有同志在问MVC中Cookie过期后如何作相关处理,他在阐 ...

  9. 4.在MVC中使用仓储模式进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...

随机推荐

  1. Metro 应用无法打开解决办法

    1.恢复默认UAC设置: 2.恢复自带防火墙服务启动类型为自动: 3.设置IE为默认浏览器(搜索和应用中): 4.如有可能使用Microsoft帐户,而不是本地帐户(仅使用有限的几个Metro应用): ...

  2. c++随机数生成

    算机的随机数都是由伪随机数,即是由小M多项式序列生成的,其中产生每个小序列都有一个初始值,即随机种子.(注意: 小M多项式序列的周期是65535,即每次利用一个随机种子生成的随机数的周期是65535, ...

  3. unity客户端与c++服务器之间的简单通讯_1

    // 服务器 # pragma once using namespace std; # include <iostream> # include <string> # incl ...

  4. 关于oc中出现的typedef的用法/定义函数指针

    typedef int (^calculateBlock)(int a,int b); 这里面typedef的作用只是给 calculateBlock取了一个 别名,说明以后可以直接使用. calcu ...

  5. Hbase与hive整合

    //hive与hbase整合create table lectrure.hbase_lecture10(sname string, score int) stored by 'org.apache.h ...

  6. PLSQL_数据泵定参数批量导入多表Expdp/Impdp Parfile(案例)

    2015-04-01 Created By BaoXinjian

  7. OAF_MDS系列1_OAF页面元数据结构MDS的解析(概念)

    2014-06-06 Created By BaoXinjian

  8. HDU 3001 Traveling(状压DP)

    题目大意:10个点的TSP问题,但是要求每个点最多走两边,不是只可以走一次,所以要用三进制的状态压缩解决这个问题.可以预处理每个状态的第k位是什么. 原代码链接:http://blog.csdn.ne ...

  9. python(13)线程池:threading

    先上代码: pool = threadpool.ThreadPool(10) #建立线程池,控制线程数量为10 reqs = threadpool.makeRequests(get_title, da ...

  10. myBatis出现Mapped Statements collection already contains value for

    造成这个问题的原因很多,我遇到的主要是因为 <select id="queryCountfor****" parameterType="java.lang.Inte ...