Models
public class DictContext:DbContext { public DictContext():base("DefaultConnection") { } public DbSet<Dict> Dict { get; set; } } public class Dict { public int Id { get; set; } public string English { get; set; } public string Chinese { get; set; } }
Controlls
public ActionResult Dict(FormCollection form, string value) { DictContext Db = new DictContext(); List<Dict> list = Db.Dict.ToList(); var collection = list.Where(a => a.English == value); foreach (var item in collection) { ViewBag.Result = item.English + "-" + item.Chinese; } ViewBag.Message = value; if (value == null || value.Length <= 2) value = " "; var Query = list.Where(a => a.English.Contains(value)); ViewData["list"] = Query.ToList(); ViewBag.Num = Query.ToList().Count; return View(); }
Views
<h2>英语词典</h2> <div>@ViewBag.Result</div> @using (Html.BeginForm("Dict", "Apply", FormMethod.Get)) { <div> @Html.TextBox("value") <input type="submit" value="查询" /> </div> <div>找到相关"@ViewBag.Message"的结果共 @ViewBag.Num 项</div> foreach (var item in ViewData["list"] as List<Gift.Models.Dict>) { <div>@item.English - @item.Chinese </div> } }