View中代码:
<input type="text" class="searchText" id="searchText"/> <input type="button" value="" onclick="Cheak()" class="searchBtn" id="searchBtn"/> <script type="text/javascript"> function Cheak() { var keys = $("#searchText").val(); if (keys.trim() == "") { alert("请输入要搜索的内容"); return false; } if (keys == "--" || keys == "'") { alert("请勿输入非法字符"); return false; } window.location.href = "/Search/SearchList/"+keys.toString(); } </script>
Controller中代码:
public ActionResult SearchList(string key) { if (key.Trim() != "" && key.IndexOf("'") < 0 && key.IndexOf("--") < 0) { var pro = (from ps in db.ProductInfoes where ps.productName.Contains(key) orderby ps.inDate descending select ps).ToList(); if (pro.Count() > 0) { StringBuilder proList = new StringBuilder(); foreach (var p in pro) { proList.Append("<li><a href="/Product/Details/" + p.ID + "/" + p.productType + "">"); proList.Append("<div class="infoImg"><img src="/Content/productImg/" + p.productPicTName + ""/></div>"); proList.Append("<div class="infoName">" + p.productName + "</div>"); proList.Append("</a></li>"); } ViewData["proLists"] = proList.ToString(); } else ViewData["proLists"] = "没有匹配的内容!"; } return View(); }
简单的搜索功能,有待改进...