您的位置:首页 > 其它

[.NET MVC4 入门系列05]添加自定义查询页Search

2013-05-14 11:39 302 查看
一、简介&目标:

这一节中,学习添加Search方法和Search视图.

方法名:SearchIndex

视图路径:/Movies/SearchIndex

功能:用户可以通过关键字查找自己感兴趣的电影

提供两种查询条件:电影名关键字、电影种类,如图


http://localhost:54782/Movies/SearchIndex?movieGenre=%E9%AD%94%E5%B9%BB&SearchString=2
这个是查询时,生成的的URL,GET方式,包含QueryString作为查询条件:“?movieGenre=%E9%AD%94%E5%B9%BB&SearchString=2”

二、添加控制器Action方法:

查询Search不需要对数据进行更改,所以Action方法只需要使用Get方式即可,所以,在Movie的Controller代码中,添加下面方法:

//
//GET: /Movies/SearchIndex
public ActionResult SearchIndex(string movieGenre,string searchString)
{
//准备种类列表数据源GereLst
var GenreLst = new List<string>();
var GenreQry = from d in db.Movies
orderby d.Genre
select d.Genre;
GenreLst.AddRange(GenreQry.Distinct());

ViewBag.movieGenre = new SelectList(GenreLst);

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))
{
return View(movies);
}
else
{
return View(movies.Where(x=>x.Genre==movieGenre));
}

return View(movies);
}


【代码解析】

  1.方法前面没有[HttpPost]所以,是Get方式向服务器传值,值应该包含在URL的QueryString中,对应方法的两个参数:

    string movieGenre(电影种类),string searchString(电影名关键字);

  2. GenreLst,用来填充查询条件中的电影种类下拉列表,查处后,通过ViewBag.movieGenre传递给View;

  3. movies最多可能会有三次赋值:

 第一次赋值获取到所有电影,这个赋值必定执行;

 第二次赋值条件是电影名关键字不为空,赋值后获取到电影命中包含指定关键字电影信息;

 第三次赋值条件是指定电影种类,赋值后获取到指定电影类型的电影信息。

4. 最终,返回到符合查询条件的电影查询视图。

三、添加View

  新建视图到/movies/views/中,配置如图:

  


更改View中代码:

@model IEnumerable<MvcApplication1.Models.Movie>

@{
ViewBag.Title = "SearchIndex";
}

<h2>SearchIndex</h2>

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

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Title)
</td>
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}

</table>


  【代码解析】

  核心代码:

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


  1.Html.BeginForm("SearchIndex","Movies",FormMethod.Get)

  HtmlHelper的BeginForm方法,用来按指定的方式向服务器提交信息,并开始Form表单。

  包含三个参数:

    第一个:“SearchIndex”,控制器中指定的Action方法;

    第二个:“Movies”,控制器名称;

    第三个:FormMethod.Get,向服务器提交信息的方式,Get方式

  2.Form表单中包含的就是那个<p>,其中包含三部分:

   1)电影类型下拉列表:Genre:@Html.DropDownList("movieGenre", "All");

     通过movieGenre(Controller的Action方法中通过ViewBag赋值)填充,默认项显示:All

   2)文本框,用来获取电影名称关键字:Title:@Html.TextBox("SearchString")

   3)提交按钮

  3.下面的table是在创建视图时,选定的List模板自动生成的,其中的绑定项是选择强类型Movie类自动指定的。

四:扩展阅读:

Lambda表达式:s=>s.Title,本文中,使用在Linq查询的方法中,充当参数:

  movies = movies.Where(s => s.Title.Contains(searchString));

  此查询对应的Sql语句为:

select * from movie where title like '%'+@searchString+'%'


初学MS 的MVC 4,参照微软www.asp.net/mvc 中的入门项目,写个MVC 4的入门系列,以供复习和分享。

微软入门项目:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

【目录】

1.[.NET MVC4 入门系列01]Helloworld MVC 4 第一个MVC4程序

2. [.NET MVC4 入门系列02]MVC Movie 为项目添加Model

3. [.NET MVC4 入门系列03]使用Controller访问Model中数据

4. [.NET MVC4 入门系列04]Controller和View间交互原理

5. .NET MVC4 入门系列05]添加自定义查询页Search

6. [.NET MVC4 入门系列06] 在Movie Model和表中添加新字段(Code First Migrations)

7. [.NET MVC4 入门系列07] 在Model模型模块中添加验证

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: