以下是我的家庭控制器的操作方法索引,它从名为AllMovies的SQL Server数据库视图中获取数据。
using Skylink.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
namespace Skylink.Controllers
{
public class HomeController : Controller
{
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
MovieContext movieContext = new MovieContext();
var movies = from m in movieContext.Movies1.ToList()
select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString)
|| s.Title.Contains(searchString));
}
switch (sortOrder)
{
case "name_desc":
movies = movies.OrderByDescending(s => s.Title);
break;
case "date":
movies = movies.OrderBy(s => s.Print);
break;
default:
movies = movies.OrderByDescending(s => s.DateAdd);
break;
}
int pageSize = 24;
int pageNumber = (page ?? 1);
return View(movies.ToPagedList(pageNumber, pageSize));
}
以下是上述操作结果返回的索引视图(Razor)的cshtml。
@using Skylink.Models;
@model PagedList.IPagedList<Skylink.Models.Movie1>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{ViewBag.Title = "Recently Added";
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/StyleSheet4.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")"></script>
</head>
<body>
<div>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</div>
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<p>
Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
@foreach (var item in Model)
{
<div class="img">
<a href="@Url.Action("Details","Home", new { id = item.Id })"><img src="@Url.Content(@item.ImageUrl)" alt="Image Not Available" /></a>
<div class="desc" style="font-family:Arial;font-size:x-small; color:white">@Html.DisplayFor(model => item.Title)</div>
<div class="desc" style="font-family:Arial;font-size:xx-small; color:yellow">IMDB: @Html.DisplayFor(model => item.Rating)</div>
<div class="desc" style="font-family:Arial;font-size:xx-small; color:white">@Html.DisplayFor(model => item.Print)</div>
</div>
}
</body>
</html>
一切正常,但运行时的搜索字符串与案例不匹配,例如我有一个电影名称“Ted”但是当我在搜索文本框中键入“ted”时,我没有得到任何结果。我必须输入完全“泰德”才能找到这部电影。我只需输入“ted”并获得结果。
希望我详细解释了我的问题。
答案 0 :(得分:3)
为什么不使用ToLower
进行比较?
switch (sortOrder.ToLower()) {
case "name_desc":
movies = movies.OrderByDescending(s => s.Title);
break;
case "date":
movies = movies.OrderBy(s => s.Print);
break;
default:
movies = movies.OrderByDescending(s => s.DateAdd);
break;
}
或者,您可以使用:
string.Equals(a, b, StringComparison.CurrentCultureIgnoreCase);
顺便说一句,使用==
进行字符串相等性评估错误。它检查参考平等,而不是词汇平等。您需要使用Equals
。有关原因,请参阅this回答。
修改,因为OP澄清了问题......如果这是您遇到问题的行:
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString)
|| s.Title.Contains(searchString));
}
(顺便说一句,那个||
子句检查相同的条件两次!!)...这是建议的修复:
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.IndexOf(searchString,
StringComparison.CurrentCultureIgnoreCase) != -1);
}