在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. gcc中动态库和静态库的链接顺序

    so文件:动态库a文件: 静态库exe文件:可执行程序(linux下以文件属性来标示是否是可执行文件,与后缀名无关) 经过自己写的一些测试程序,大致了解了下gcc中链接顺序问题,总结出以下几点:1,动 ...

  2. 可以返回执行结果的system函数加强版本

    在GNU Linux C编程中,要想进行系统命令的执行的话,只提供了system接口,但是此接口并不能得到命令执行后所输出的值,而只能够得到命令是否执行成功的结果.仅仅这样的功能还是不够的,有的时候是 ...

  3. mssql查询某个值存在某个表里的哪个字段的值里面

    第一步:创建 查询某个值存在某个表里的哪个字段的值里面 的存储过程 create proc spFind_Column_In_DB ( @type int,--类型:1为文字类型.2为数值类型 )-- ...

  4. Akka(一) - akka的wordcount

    1. 启动类 object Application extends App{ val _system = ActorSystem("HelloAkka") //构建akka容器 v ...

  5. svn 提交错误 400 Bad Reqest MKACTIVITY 请求于XX失败 Conflict Unable to connect to a repository at URL

    思路来源:http://www.cnblogs.com/wangyt223/archive/2012/11/22/2782801.html svn 提交错误 400 Bad Reqest MKACTI ...

  6. C和C++混合编程

    extern "C"表示编译生成的内部符号名使用C约定.C++支持函数重载,而C不支持,两者的编译规则也不一样.函数被C++编译后在符号库中的名字与C语言的不 同.例如,假设某个函 ...

  7. PLSQL_性能优化系列07_Oracle Parse Bind Variables解析绑定变量

    2014-09-25 Created By BaoXinjian

  8. centos下的一些命令

    1.查看版本 cat /etc/redhat-release 2.安装VIM yum -y install vim-enhanced 3.升级系统 yum -y update 4.把 vi 替换为 v ...

  9. Python中if __name__ == "__main__": 的作用

    在很多python脚本中在最后的部分会执行一个判断语句if __name__ == "__main__:",之后还可能会有一些执行语句.那添加这个判断的目的何在? 在python编 ...

  10. 如何用js来判断浏览器类型(ie,firefox)等等

    现在网络上的浏览器,操作系统就象中国的方言一样,那个叫多啊!这给我们这些开发人员 带来了巨大的痛苦!虽然可能大家的喜好不同!用的系统也不同!有人喜欢用ie,有人喜欢用 firefox,还有人喜欢用腾讯 ...