我正在尝试使用 C# 验证 MVC 3 中的搜索框,以便当用户不输入任何内容时,ViewBag 中会出现一条消息,说“请输入搜索查询”。任何帮助将不胜感激。
public ViewResult Index(string sortOrder, String searchString)
{
ViewBag.NamesSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
ViewBag.UserSortParm = sortOrder == "UserID" ? "Name" : "Song";
var User = from s in db.User select s;
if (!String.IsNullOrEmpty(searchString))
{
User = Users.Where(s => s.Name.ToUpper().Contains(searchString.ToUpper())
|| s.Songs.ToUpper().Contains(searchString.ToUpper()));
}
switch (sortOrder)
{
case "Name":
Users = Users.OrderByDescending(s => s.Name);
break;
case "Songs":
Users = Users.OrderByDescending(s => s.Song);
break;
default:
Users = Users.OrderBy(s => s.Name);
break;
}
// ...
}