在 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/ 系列目录: ...
随机推荐
- ARM Linux从Bootloader、kernel到filesystem启动流程
转自:http://www.veryarm.com/1491.html ARM Linux启动流程大致为:bootloader ---->kernel---->root filesyste ...
- 创建 Windows 7/8 的计算机修复光盘或工具
Windows 7 Windows 8 控制面板--系统和安全--操作中心--恢复
- linux svn迁移备份的三种方法
原文:http://www.iitshare.com/linux-svn-migration.html svn备份方式对比分析 一般采用三种方式: 1.svnadmin dump 2.svnadmin ...
- MVC:上传文件
今天写了一个使用MVC上传的DEMO,很简单不超过10行代码.代码如下(关注重点,所以尽量精简掉其他代码): 项目结构
- DISPOSE_ON_CLOSE 和 EXIT_ON_CLOSE 的区别
If you have several JFrames open and you close one that has EXIT_ON_CLOSE it will close all the JFra ...
- (VS TFS) Adding existing project to solution in TFS.
正常的情况话,直接加入project,然后选择"Source control" -> “Add selected projects to source control.... ...
- SpringAOP所支持的AspectJ切点指示器
在spring中尝试使用AspectJ其他指示器时,将会抛出IllegalArgumentException异常. 当我们查看上面展示的这些spring支持的指示器时,注意只有execution指示器 ...
- CF 369C . Valera and Elections tree dfs 好题
C. Valera and Elections The city Valera lives in is going to hold elections to the city Parliament ...
- 【java】简单的事件总线EventBus
public class EventBus { private static Map<String, EventListener> eventListeners = new HashMap ...
- REST架构实质(转)
REST(Representational State Transfer) 曾经被误解为只是CRUD(增删改查),从这个层面上,好像REST只是和RPC一个层面的东西,没有什么了不起,其实这些都是对R ...