在 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>, "---请选择类型---")
}
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 中使用下拉列表的更多相关文章
- 基于MVC4+EasyUI的Web开发框架经验总结(6)--在页面中应用下拉列表的处理
在很多Web界面中,我们都可以看到很多下拉列表的元素,有些是固定的,有些是动态的:有些是字典内容,有些是其他表里面的名称字段:有时候引用的是外键ID,有时候引用的是名称文本内容:正确快速使用下拉列表的 ...
- asp.net mvc中DropDownList
asp.net mvc中DropDownList的使用. 下拉列表框 以分为两个部分组成:下拉列表和默认选项 DropDownList扩展方法的各个重载版本基本上都会传递到这个方法上: publi ...
- .NetCore MVC中的路由(2)在路由中使用约束
p { margin-bottom: 0.25cm; direction: ltr; color: #000000; line-height: 120%; orphans: 2; widows: 2 ...
- .NetCore MVC中的路由(1)路由配置基础
.NetCore MVC中的路由(1)路由配置基础 0x00 路由在MVC中起到的作用 前段时间一直忙于别的事情,终于搞定了继续学习.NetCore.这次学习的主题是MVC中的路由.路由是所有MVC框 ...
- Asp.Net MVC中使用StreamReader读取“Post body”之应用场景。
场景:有三个市场(Global.China.USA),对前台传过来的数据有些验证需要细化到每个市场去完成. 所以就出现了基类(Global)和派生类(China.USA) 定义基类(Global)Pe ...
- 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...
- ASP.NET Core MVC 中的 [Controller] 和 [NonController]
前言 我们知道,在 MVC 应用程序中,有一部分约定的内容.其中关于 Controller 的约定是这样的. 每个 Controller 类的名字以 Controller 结尾,并且放置在 Contr ...
- ASP.NET MVC中利用AuthorizeAttribute实现访问身份是否合法以及Cookie过期问题的处理
话说来到上海已经快半年了,时光如白驹过隙,稍微不注意,时间就溜走了,倒是没有那么忙碌,闲暇之际来博客园还是比较多的,记得上次在逛博问的时候看到有同志在问MVC中Cookie过期后如何作相关处理,他在阐 ...
- 4.在MVC中使用仓储模式进行增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...
随机推荐
- SQL常用方法整理
去除字符串重复项: declare @str varchar(8000) declare @ret varchar(8000),@return varchar(8000) select @str = ...
- 回到顶部缓动效果代码 --- tween动画函数库
function animateGoTop() { var top = $(document).scrollTop(); var end = 0; var dur = 500; var t = 0; ...
- C#绘制立体三维饼状图
转载自原文 C#绘制立体三维饼状图(超酷) 内容原文地址:LINK [翻译]Julijan Sribar著3D Pie Chart一个用于绘制立体三维饼状图的C#类库[简介]本文的想法就是创建一个独立 ...
- 设置Excel的自动筛选功能
单元格数字格式的问题 NPOI向Excel文件中插入数值时,可能会出现数字当作文本的情况(即左上角有个绿色三角),这样单元格的值就无法参与运算.这是因为在SetCellValue设置单元格值的时候使用 ...
- Idea KeyGen
import java.math.BigInteger; import java.util.Date; import java.util.Random; import java.util.Scanne ...
- laravel 删除一条migration后要执行composer命令
Laravel 删除一条migration 字数29 阅读30 评论0 喜欢0 如果迁移已经执行,先回滚php artisan migrate:rollback 然后删除迁移文件,运行composer ...
- 关于Objective-C 对象release操作的一个小问题探讨
来源:http://blog.csdn.net/duxinfeng2010/article/details/8757211 最近遇到这样一个问题,以前的时候并未注意:新建一个工程,然后添加一个类,文件 ...
- ORA-30036
http://blog.sina.com.cn/s/blog_676255e101018d5s.html
- 常用开源Jabber(XMPP) IM服务器介绍(转)
1. Openfire (Wildfire) 3.x授权: GPL or 商用操作系统平台:所有(使用Java开发)XMPP Jabber 协议实现情况:98%Tim 评价:安装和使用非常简单,安 ...
- .NET Session操作
public class SessionHelper { /// <summary> /// 根据session名获取session对象 /// </summary> /// ...