A.创建mvc4项目,Basic类型.
B.创建Model:
Student.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcSearchTest.Models
{
public class Student
{
public int ID { set; get; }
public string Name { set; get; }
public int Age { set; get; }
public bool Male { set; get; }
}
}
C.创建控制器:
HomeController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcSearchTest.Models;
namespace MvcSearchTest.Controllers
{
public class HomeController : Controller
{
private MvcSearchTestContext db = new MvcSearchTestContext();
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult Search(string Name,int? Age)
{
int age ;
age = Age??-1;
if (string.IsNullOrEmpty(Name) != true && string.IsNullOrWhiteSpace(Name) != true)
{
//linq方式,查询所有影片,仅定义,不执行
//var students = from m in db.Students
// select m;
var students = db.Students;
if (age == -1)
{
var results = students.Where(m => m.Name == Name).OrderByDescending(m => m.Age).ThenBy(m=>m.Name);
return View(results.ToList());
}
else
{
var results = students.Where(m => m.Name == Name).Where(m=>m.Age==age).OrderBy(m => m.Age);
return View(results.ToList());
}
}
ViewBag.Tip = "Name不能为空。";
return View("Index");
}
}
}
StudentController.cs:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcSearchTest.Models;
namespace MvcSearchTest.Controllers
{
public class StudentController : Controller
{
private MvcSearchTestContext db = new MvcSearchTestContext();
//
// GET: /Student/
public ActionResult Index()
{
return View(db.Students.ToList());
}
//
// GET: /Student/Details/5
public ActionResult Details(int id = 0)
{
Student student = db.Students.Find(id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
//
// GET: /Student/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Student/Create
[HttpPost]
public ActionResult Create(Student student)
{
if (ModelState.IsValid)
{
db.Students.Add(student);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}
//
// GET: /Student/Edit/5
public ActionResult Edit(int id = 0)
{
Student student = db.Students.Find(id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
//
// POST: /Student/Edit/5
[HttpPost]
public ActionResult Edit(Student student)
{
if (ModelState.IsValid)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}
//
// GET: /Student/Delete/5
public ActionResult Delete(int id = 0)
{
Student student = db.Students.Find(id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
//
// POST: /Student/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Student student = db.Students.Find(id);
db.Students.Remove(student);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
D.创建相应的View:
Home/Index.cshtml:
@{
ViewBag.Title = "Index";
}
<h2>Search Demo</h2>
<h2>@ViewBag.Tip</h2>
@using (Html.BeginForm("Search","Home"))
{
<p>Name: @Html.TextBox("Name")<br /> </p>
<p>Age: @Html.TextBox("Age")<br /> </p>
<input type="submit" value="查询" />
}
<p>@Html.ActionLink("学生管理", "Index", "Student")</p>
Home/Search.cshtml:
@model IEnumerable<MvcSearchTest.Models.Student>
@{
ViewBag.Title = "Search";
}
<h2>Search Result</h2>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th>
@Html.DisplayNameFor(model => model.Male)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.DisplayFor(modelItem => item.Male)
</td>
</tr>
}
</table>
提示:
1.利用Student类及平台的内置List模板产生Search.chhtml文件,
2.,利用Student类及Entity Framework相关技术创建StudentController.cs文件,并且产生数据库上下文及相应的数据库,再利用平台相关技术产生Student类相应的View文件.Index,Edit,Create,Delete,Detail等,如下图所示: