您的位置:首页 > 编程语言 > ASP

ASP.NET MVC 学习4、Controller中添加SearchIndex页面,实现简单的查询功能

2014-03-10 17:10 1031 查看
参考:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view

本文内容:

1,熟悉MVC的路由过程,URL如果导向到Controller相应的方法中

2,新增SearchIndex页面,实现简单的查询功能

http://localhost:9898/Movies,鼠标移动到”Edit”上面的时候,我们看到Edit将要导向的路径:

<system.web>
<globalization culture ="en-US" />
<!--elements removed for clarity-->
</system.web>


View Code
所有的HttpGet方法都是类似的。他们得到一个Movie对象,然后传递给View,Get:不应该改变页面中的数据

下面我们添加查询页面 ,Adding a Search Method and Search View

1,Controller中添加SearchIndex方法():

public ActionResult SearchIndex(string MovieGenre, string SearchString)
{
var GenreLst = new List<string>();
//Select All Genre
var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.MovieGenre = new SelectList(GenreLst); // 把List传递给ViewBag
var movies = from m in db.Movies
select m;
//SearchString 字符串如果不为空,Movies filter 包含SearchString的对象
if (!string.IsNullOrEmpty(SearchString))
{
movies = movies.Where(s => s.Title.Contains(SearchString));
}
if (string.IsNullOrEmpty(MovieGenre))
//如果MovieGenre为空,直接返回movies
return View(movies);
else
{
//Movie不为空,filter Genre
return View(movies.Where(x => x.Genre == MovieGenre));
}
}


2,SearchIndex方法中右键点击,添加视图





3,SearchIndex.cshtml页面中添加以下代码:

@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm("SearchIndex", "Movies", FormMethod.Get))
{
<p>
Genre: @Html.DropDownList("movieGenre", "All")
Title: @Html.TextBox("SearchString")
<input type="submit" value="Filter" />
</p>
}


4,查询页面实现:



查询页面的,需要熟悉的东西:Linq语句查询,参数传递,View中HTML Helper中TextBoxt,DropDownList等控件的书写
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐