目录
[编辑 注意这里要写Home编辑](#编辑 注意这里要写Home编辑)
[编辑 7HomeController.cs代码](#编辑 7HomeController.cs代码)
效果图
实现过程
1创建数据库
2创建项目文件
3创建控制器,右键添加,控制器
注意这里要写Home
创建成功
数据模型创建过程之前作品有具体过程
4创建DAL
cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication1.Models;
namespace MvcApplication1.DAL
{
public class PersonDAL
{
public static List<Models.Users> Find(string name) {
PersonDBEntities db = new PersonDBEntities();
return db.Users.ToList().Where(x => x.Name.ToString().Contains(name)).ToList();
}
}
}
5创建BLL
cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.BLL
{
public class PersonBLL
{
public static List<Models.Users> Find(string name)
{
return DAL.PersonDAL.Find(name);
}
}
}
6创建视图,右键添加视图
7HomeController.cs代码
cpp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index(string name)
{
if (name==null)
{
PersonDBEntities db = new PersonDBEntities();
ViewData["users"] = db.Users.ToList();
}
else
{
ViewData["users"] = BLL.PersonBLL.Find(name);
}
ViewData["xianshi"] = name;
return View();
}
}
}
8Index.cshtml代码
html
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" action="/Home/Index">
<label>模糊查找姓名</label>
<input type="text" name="name" value="@ViewData["xianshi"]" />
<input id="Submit1" type="submit" value="查找"/>
</form>
<div>
<table border="1">
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
</tr>
@{
foreach (var item in ViewData["users"] as List<MvcApplication1.Models.Users>)
{
<tr>
<td>@item.ID</td>
<td>@item.Name</td>
<td>@item.Age</td>
</tr>
}
}
</table>
</div>
</body>
</html>