ASP.NEt MVC5--创建下拉列表
Adding Search by Genre
If you added the HttpPost version of the Index method, delete it now.
Next, you'll add a feature to let users search for movies by genre. Replace the Index method with the following code:
现在我们来改写Index方法,
public ActionResult Index(string movieGenre,string searchString)
{
var GenreLst = new List<string>(); var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.Distinct());
//SelectList生成列表
ViewBag.movieGenre = new SelectList(GenreLst); //Linq查询
var movies = from m in db.Movies select m; if (!string.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
if (string.IsNullOrEmpty(movieGenre))
{
movies = movies.Where(x => x.Genre == movieGenre);
}
return View(movies); }
var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
当执行完这句代码之后,SQL(GenreQry)为:
SELECT
[Extent1].[Genre] AS [Genre]
FROM [dbo].[Movies] AS [Extent1]
ORDER BY [Extent1].[Genre] ASC
var movies = from m in db.Movies select m;
执行完这句代码之后,SQL(Movies)为:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Title] AS [Title],
[Extent1].[ReleaseDate] AS [ReleaseDate],
[Extent1].[Genre] AS [Genre],
[Extent1].[Price] AS [Price]
FROM [dbo].[Movies] AS [Extent1]
现在先让我们,在Index视图页面中,添加一个实现下拉框的代码吧:

In the following code:
@Html.DropDownList("movieGenre", "All")
The parameter "movieGenre" provides the key for the DropDownList helper to find a IEnumerable<SelectListItem > in the ViewBag. The ViewBag was populated in the action method:
这句话的意思是:moviegenre参数,为DropDownList方法,提供了主键,去找到在控制器中定义的Viewbag.movieGenre
The parameter "All" provides the item in the list to be preselected. Had we used the following code:
这句话的意思是:All参数,提供了默认选择项
效果图:
In this section you created a search action method and view that let users search by movie title and genre. In the next section, you'll look at how to add a property to the Movie model and how to add an initializer that will automatically create a test database.
通过下拉列表,来搜索:
通过下拉列表和title输入框搜索:
ASP.NEt MVC5--创建下拉列表的更多相关文章
- 五、 创建连接串连接本地数据库(ASP.NET MVC5 系列)
1. 创建连接串连接本地SQLServer数据库 上节讲到MovieDBContext类,这个类的作用是连接数据库并将Movie对象迁移到数据库记录中.不过你会问一个问题:如何知道这个对象将连接哪个数 ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](一)
前言 大家好,我是Rector 从今天开始,Rector将为大家推出一个关于创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar]的文章系列, ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](五)
前言 Hi,大家好,我是Rector 时间飞逝,一个星期又过去了,今天还是星期五,Rector在图享网继续跟大家分享系列文本:一步一步创建ASP.NET MVC5程序[Repository+Autof ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](四)
前言 上一篇<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](三)>,我们完成了: * 引用SqlSugar * ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](三)
前言 上一篇<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](二)>我们通过如下操作: 创建实体及工具类 创建Re ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](二)
前言: 在本系列第一篇<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](一)>中,我为大家介绍了搭建空白解决方案以 ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](六)
前言 大家好,我是Rector 又是星期五,很兴奋,很高兴,很high...啦啦啦... Rector在图享网又和大家见面啦!!!上一篇<一步一步创建ASP.NET MVC5程序[Reposit ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](七)
前言 大家好,我依旧是你们的老朋友Rector,很高兴又在周五的时候准时和大家见面. Rector的系列文章[一步一步创建ASP.NET MVC5程序[Repository+Autofac+Autom ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](八)
前言 Hi, 大家好,还是星期五,还是Rector,又在图享网准时和大家见面了. 今天给大家带来系列教程<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Auto ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](九)
前言 童鞋们,大家好 我是专注.NET开发者社区建设的实践者Rector. 首先,为自己间隔了两个星期五再更新本系列文章找个不充分的理由:Rector最近工作,家庭的各种事务所致,希望大家谅解. 本文 ...
随机推荐
- Java 7 jstat – JVM Statistics Monitoring Tool【翻译】
原文地址:Java 7 jstat 本文内容 语法 参数 描述 虚拟机标识符 选项 一般选项 输出选项 示例 先发出来,然后慢慢翻译~ 语法 jstat [ generalOption | outpu ...
- DropDownList 获取不了选择的值 这种错误
有时候做项目的时候 发现DropDownList 获取不了选择的值 这个原因很可能是 你初始化DropDownList的时候 没有进行 ispostback的判断 导致提交的时候 又初始化了一次... ...
- 倒计时,js
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 仿iOS Segmented Control样式"
同步发表于http://avenwu.net/2015/02/05/styled_radiogroup_segmented_control Fork on github https://github. ...
- 菜鸟教程之工具使用(十)——用BlazeMeter录制JMeter测试脚本
工具: 1,JMeter 2,Chrome 3,BlazeMeter 4,SwitchyOmega(如果需要代理) 步骤: 以上工具准备好以后就可以录制JMeter的测试脚本了, 在Chrome中点击 ...
- jQuery Mobile 移动开发中的日期插件Mobiscroll使用说明
近期在移动方面的开发,使用jQuery Mobile ,移动方面的插件不如Web 方面的插件多,选择的更少,有一些需要自己去封装,但功力尚不足啊. 日期插件JQM也提供了内置的,但样式方面不好看,只好 ...
- Hadoop2.x源码-编译剖析
1.概述 最近,有小伙伴涉及到源码编译.然而,在编译期间也是遇到各种坑,在求助于搜索引擎,技术博客,也是难以解决自身所遇到的问题.笔者在被询问多次的情况下,今天打算为大家来写一篇文章来剖析下编译的细节 ...
- string-->wstring-->string
std::string src("三毛三毛三毛三毛三三三三流浪记"); size_t size = mbstowcs(NULL,src.c_str(),0); std::wstri ...
- 结合使用saiku、mondrian workbentch建立多维查询报表
1.简介 前几篇博客已经介绍了saiku.mondrian.MDX和模式文件他们之间的关系,那么如何将它们串联起来,供产品人员使用哪?下面咱们一步一步的实现 2.建立数据表 建表语句参考:http:/ ...
- smdkv210
参考:http://code.google.com/p/libyuv/issues/detail?id=295 ******************************************** ...