我无法想象如何通过任何字来进行搜索功能搜索,即描述字段只想搜索一个单词或一个以字开头的单词。我记得我在sql命令中使用了Like但不知道如何在这里使用它 Kbs控制器
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using _399KB.Models;
using PagedList;
namespace _399KB.Controllers
{
public class kbsController : Controller
{
private kbEntities db = new kbEntities();
// GET: kbs
public ActionResult Index(string searchBy, string search, int? page)
{
if
(searchBy == "title")
return View(db.kbs.OrderBy(x => x.title).Where(x => x.title.StartsWith(search) || search == null).ToList().ToPagedList(page ?? 1, 5));
if
(searchBy == "description")
return View(db.kbs.OrderBy(x => x.title).Where(x => x.description.StartsWith(search) || search == null).ToList().ToPagedList(page ?? 1, 5));
if
(searchBy == "keywords")
return View(db.kbs.OrderBy(x => x.title).Where(x => x.keywords.StartsWith(search) || search == null).ToList().ToPagedList(page ?? 1, 5));
if
(searchBy == "links")
return View(db.kbs.OrderBy(x => x.title).Where(x => x.links.StartsWith(search) || search == null).ToList().ToPagedList(page ?? 1, 5));
else
{
return View(db.kbs.OrderBy(x => x.title).Where(x => x.keywords.StartsWith(search) || search == null).ToList().ToPagedList(page ?? 1, 5));
}
}
// GET: kbs/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
kb kb = db.kbs.Find(id);
if (kb == null)
{
return HttpNotFound();
}
return View(kb);
}
// GET: kbs/Create
public ActionResult Create()
{
return View();
}
// POST: kbs/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "id,title,description,keywords,links")] kb kb)
{
if (ModelState.IsValid)
{
db.kbs.Add(kb);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(kb);
}
// GET: kbs/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
kb kb = db.kbs.Find(id);
if (kb == null)
{
return HttpNotFound();
}
return View(kb);
}
// POST: kbs/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "id,title,description,keywords,links")] kb kb)
{
if (ModelState.IsValid)
{
db.Entry(kb).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(kb);
}
// GET: kbs/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
kb kb = db.kbs.Find(id);
if (kb == null)
{
return HttpNotFound();
}
return View(kb);
}
// POST: kbs/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
kb kb = db.kbs.Find(id);
db.kbs.Remove(kb);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
查看页面
@using PagedList;
@using PagedList.Mvc;
@using _399KB.Models;
@model IPagedList<_399KB.Models.kb>
@{
ViewBag.Title = "Index";
}
<hr />
<!-- ** BEGIN SEARCH AREA ** -->
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title">Search Area</h1>
</div>
<div class="panel-body">
<div class="form-group">
@using (Html.BeginForm("Index", "kbs/Index", FormMethod.Get))
{
@Html.RadioButton("searchBy", "keywords", true)<text>keywords</text>
@Html.RadioButton("searchBy", "title")<text>title </text>
@Html.RadioButton("searchBy", "description")<text>description </text>
@Html.RadioButton("searchBy", "links")<text>links</text>
<div class="panel-body">
@Html.TextBox("search", null, new { @class = "form-control" })<span class="input-group-btn"></span>
</div>
<div class="panel-footer">
<button id="btnSearch"
class="btn btn-sm btn-primary">
<i class="glyphicon glyphicon-search"></i>
Search
</button>
<a href="@Url.Action("Index", "kbs")" class="btn btn-sm btn-primary">
<span class="glyphicon glyphicon-cog" aria-hidden="true"></span>
Reset</a>
<a href="@Url.Action("Create", "kbs")" class="btn btn-sm btn-primary">
<span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
Add
</a>
</div>
}
</div>
</div>
</div>
<!-- ** END SEARCH AREA ** -->
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().title)
</th>
<th>
@Html.DisplayNameFor(model => model.First().description)
</th>
<th>
@Html.DisplayNameFor(model => model.First().keywords)
</th>
<th>
@Html.DisplayNameFor(model => model.First().links)
</th>
<th>Modify_KB_Record___</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.DisplayFor(modelItem => item.description)
</td>
<td>
@Html.DisplayFor(modelItem => item.keywords)
</td>
<td>
<a href="@Html.DisplayFor(modelItem => item.links)">@Html.DisplayFor(modelItem => item.links)</a>
</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>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["search"] }),
new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true, DisplayItemSliceAndTotal = true })
答案 0 :(得分:0)
而不是StartsWith
,请使用Contains
。