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--创建下拉列表的更多相关文章

  1. 五、 创建连接串连接本地数据库(ASP.NET MVC5 系列)

    1. 创建连接串连接本地SQLServer数据库 上节讲到MovieDBContext类,这个类的作用是连接数据库并将Movie对象迁移到数据库记录中.不过你会问一个问题:如何知道这个对象将连接哪个数 ...

  2. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](一)

    前言 大家好,我是Rector 从今天开始,Rector将为大家推出一个关于创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar]的文章系列, ...

  3. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](五)

    前言 Hi,大家好,我是Rector 时间飞逝,一个星期又过去了,今天还是星期五,Rector在图享网继续跟大家分享系列文本:一步一步创建ASP.NET MVC5程序[Repository+Autof ...

  4. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](四)

    前言 上一篇<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](三)>,我们完成了: * 引用SqlSugar * ...

  5. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](三)

    前言 上一篇<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](二)>我们通过如下操作: 创建实体及工具类 创建Re ...

  6. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](二)

    前言: 在本系列第一篇<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](一)>中,我为大家介绍了搭建空白解决方案以 ...

  7. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](六)

    前言 大家好,我是Rector 又是星期五,很兴奋,很高兴,很high...啦啦啦... Rector在图享网又和大家见面啦!!!上一篇<一步一步创建ASP.NET MVC5程序[Reposit ...

  8. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](七)

    前言 大家好,我依旧是你们的老朋友Rector,很高兴又在周五的时候准时和大家见面. Rector的系列文章[一步一步创建ASP.NET MVC5程序[Repository+Autofac+Autom ...

  9. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](八)

    前言 Hi, 大家好,还是星期五,还是Rector,又在图享网准时和大家见面了. 今天给大家带来系列教程<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Auto ...

  10. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](九)

    前言 童鞋们,大家好 我是专注.NET开发者社区建设的实践者Rector. 首先,为自己间隔了两个星期五再更新本系列文章找个不充分的理由:Rector最近工作,家庭的各种事务所致,希望大家谅解. 本文 ...

随机推荐

  1. WPF Wonders: Transformations (and Robots!)

    indows Presentation Framework (WPF) gets a lot of mileage out of being layered on top of DirectX, in ...

  2. ecshop利用.htaccess实现301重定向的方法

    实现方法如下(空间必须支持对目录中的.htaccess文件解析) 打开 .htaccess 找到 RewriteEngine on 它的下方添加 RewriteCond %{HTTP_HOST} ^需 ...

  3. AppStore 相关

    App 跳转 AppStore 网址链接   https://itunes.apple.com/app/uri/id582319843?mt=8   https 可替换成 itms,可直接避免进入 S ...

  4. 申请Payoneer美国万事达信用卡,可获得一个美国虚拟银行账户,立即注册可得25美元

    申请Payoneer美国万事达信用卡,可获得一个美国虚拟银行账户,可以在国内任意一个支持万事达的ATM.POS机上取现和刷卡消费.Payoneer可以网上购物,购买国外的产品,对我们有一个好处就是利用 ...

  5. 碰到一个在app内部浏览器锚点异常的问题

    最近在做一个文章评论的功能,其中一个需求是:在提交完评论后,需要跳转到位于页面底部的评论区域,正常情况下location.href=http://m.hostname.cn/article#comme ...

  6. IOS的浅拷贝和深拷贝

    什么是深拷贝和浅拷贝 浅拷贝:就是指针的复制,拷贝的指针跟原指针指向内存中的同一个位置的对象.至于对象的引用计数值是否+1,就是看拷贝的指针赋给给的变量是Strong类型的,还是week类型的. 如果 ...

  7. VPN有什么用?(转载)

    VPN有什么用?转载自:http://www.chinaz.com/web/2012/0320/240819.shtml 当你非常非常想访问一个国外的网站,而这个网站被防火墙屏蔽的时候,你应该怎么做呢 ...

  8. 开发BI系统时的需求分析研究

    我们知道MIS,知道ERP,知道GIS等等,这些系统在管理限制上有很多的冲突,管理和被管理,开放和限制等等,然而BI在开始就不是这样的.BI要求的就是易用还要易于扩展,首先是报表,这个是你无条件的需要 ...

  9. [MONGODB]: WHEN ARBITER REQUIRED FOR REPLICA SET

    October 27, 2015 · by Srinivas · in MongoDB   MongoDB replica sets provide a number of features that ...

  10. 【原创】试用十天被Pass所带来的启示

    试用十天被Pass所带来的启示   招聘是门学问,很多人在研究,也有很多方案,不过面对人员难聘问题,很多方法又不灵了.于是我们采用了降低标准方案,扩招进来一些人员,于是问题又来了,想不想听我亲身经历的 ...