• 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方法,

     1   public ActionResult Index(string movieGenre,string searchString)
     2         {
     3             var GenreLst = new List<string>();
     4 
     5             var GenreQry = from d in db.Movies
     6                            orderby d.Genre
     7                            select d.Genre;
     8             GenreLst.AddRange(GenreQry.Distinct());
     9             //SelectList生成列表
    10             ViewBag.movieGenre = new SelectList(GenreLst);
    11 
    12             //Linq查询
    13             var movies = from m in db.Movies select m;
    14 
    15             if (!string.IsNullOrEmpty(searchString))
    16             {
    17                 movies = movies.Where(s => s.Title.Contains(searchString));
    18             }
    19             if (string.IsNullOrEmpty(movieGenre))
    20             {
    21                 movies = movies.Where(x => x.Genre == movieGenre);
    22             }
    23             return View(movies);
    24 
    25            
    26         }
      var GenreQry = from d in db.Movies
                                orderby d.Genre
                                select d.Genre;

    当执行完这句代码之后,SQL(GenreQry)为:

    1 SELECT    
    2 [Extent1].[Genre] AS [Genre]   
    3 FROM [dbo].[Movies] AS [Extent1]  
    4 ORDER BY [Extent1].[Genre] ASC

      var movies = from m in db.Movies select m;  

    执行完这句代码之后,SQL(Movies)为:

    1 SELECT   
    2 [Extent1].[ID] AS [ID],   
    3 [Extent1].[Title] AS [Title],    
    4 [Extent1].[ReleaseDate] AS [ReleaseDate],   
    5 [Extent1].[Genre] AS [Genre],    
    6 [Extent1].[Price] AS [Price]  
    7 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输入框搜索:


  • 相关阅读:
    [C#] 主窗口嵌入子窗口(绑架窗口)
    【WP7】后台加载数据BackgroundWorker
    【WP7】手势操作与拨号键盘
    【算法】拼音匹配算法(支持多音字)
    【笔记】歌词显示问题
    【笔记】使用千千静听服务器下载歌词
    【WP7】代码创建png图片
    【WP7】自定义字体
    【WP7】对象序列化
    【WP7】控件倾斜特效
  • 原文地址:https://www.cnblogs.com/caofangsheng/p/4591163.html
Copyright © 2020-2023  润新知