在UserInfo表中写入验证;
前端页面:
最上面引入:@model AspNetMVC.Models.UserInfo
AspNetMVC:项目名称,UserInfo 数据库表名
@using (Html.BeginForm())
{
用户名:
@Html.TextBoxFor(u => u.LogName)
@Html.ValidationMessageFor(u => u.LogName)
密码:
@Html.TextBoxFor(u => u.LogPwd)
@Html.ValidationMessageFor(u => u.LogPwd)
@Html.ValidationSummary(true)
}
Home控制器中:
public class HomeController : Controller
{
MVCLession31Entities db = new MVCLession31Entities();
// GET: Home
//登录
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(Login u)
{
if (ModelState.IsValid && varLogin(u.LogName,u.LogPwd))
{
return RedirectToAction("Index");
}
else
{
ModelState.AddModelError("","账号或密码错误");
}
return View();
}
private bool varLogin(string logName, string logPwd)
{
var u = (from p in db.Login where p.LogName == logName && p.LogPwd == logPwd select p).FirstOrDefault();
if (u==null)
{
return false;
}
else
{
FormsAuthentication.SetAuthCookie("logName",false);
return true;
}
}
添加Home控制器:
在引用MVCLession31Entities字段。
public class HomeController : Controller
{
MVCLession31Entities db = new MVCLession31Entities();
// GET: Home
public ActionResult Index()
{
//查询员工列表
ViewBag.EmpList= db.Employee;
return View();
}
在Index上右键添加视图到Index页面;
员工列表
添加员工
员工编号
部门编号
员工姓名
员工电话
员工区域
员工薪资
操作
@foreach (var item in ViewBag.EmpList)
{
@item.EmpId
@item.DeptId
@item.EmpName
@item.EmpPhone
@item.EmpArea
@item.EmpSalary
编辑 删除
}
查询运行结果
public ActionResult Index()
{
//ViewBag.EmpList = db.Employee;
//联合查询
ViewBag.EmpList = from e in db.Employee
join d in db.Dept on e.DeptId equals d.DeptId
select new EmployeeViewModel
{
EmpId = e.EmpId,
DeptName = d.DeptName,
EmpName = e.EmpName,
EmpPhone = e.EmpPhone,
EmpArea = e.EmpArea,
EmpSalary = e.EmpSalary
};
return View();
}
public class HomeController : Controller
{
MVCLession31Entities db = new MVCLession31Entities();
// GET: Home
public ActionResult Index()
{
//联合查询
ViewBag.EmpList = from e in db.Employee
join d in db.Dept on e.DeptId equals d.DeptId
select new EmployeeViewModel
{
EmpId = e.EmpId,
DeptName = d.DeptName,
EmpName = e.EmpName,
EmpPhone = e.EmpPhone,
EmpArea = e.EmpArea,
EmpSalary = e.EmpSalary
};
ViewBag.DeptList = db.Dept;
return View();
}
[HttpPost]
public ActionResult Index(string EmpName,int DeptId)
{
if (EmpName != "" && DeptId != 0)
{
//联合查询
ViewBag.EmpList = from e in db.Employee
join d in db.Dept on e.DeptId equals d.DeptId
where e.EmpName.Contains(EmpName)
where e.DeptId == DeptId
select new EmployeeViewModel
{
EmpId = e.EmpId,
DeptName = d.DeptName,
EmpName = e.EmpName,
EmpPhone = e.EmpPhone,
EmpArea = e.EmpArea,
EmpSalary = e.EmpSalary
};
}
else if (EmpName != "" && DeptId == 0)
{
//联合查询
ViewBag.EmpList = from e in db.Employee
join d in db.Dept on e.DeptId equals d.DeptId
where e.EmpName.Contains(EmpName)
select new EmployeeViewModel
{
EmpId = e.EmpId,
DeptName = d.DeptName,
EmpName = e.EmpName,
EmpPhone = e.EmpPhone,
EmpArea = e.EmpArea,
EmpSalary = e.EmpSalary
};
}
else if (EmpName == "" && DeptId != 0)
{
//联合查询
ViewBag.EmpList = from e in db.Employee
join d in db.Dept on e.DeptId equals d.DeptId
where e.DeptId == DeptId
select new EmployeeViewModel
{
EmpId = e.EmpId,
DeptName = d.DeptName,
EmpName = e.EmpName,
EmpPhone = e.EmpPhone,
EmpArea = e.EmpArea,
EmpSalary = e.EmpSalary
};
}
else
{
//联合查询
ViewBag.EmpList = from e in db.Employee
join d in db.Dept on e.DeptId equals d.DeptId
select new EmployeeViewModel
{
EmpId = e.EmpId,
DeptName = d.DeptName,
EmpName = e.EmpName,
EmpPhone = e.EmpPhone,
EmpArea = e.EmpArea,
EmpSalary = e.EmpSalary
};
}
ViewBag.EmpName = EmpName;
ViewBag.DeptId = DeptId;
ViewBag.DeptList = db.Dept;
return View();
}
Index查询页面添加员工姓名,部门名称及搜索按钮:
员工信息列表
添加员工
//添加员工页面
public ActionResult AddEmpForm()
{
//查询部门列表
ViewBag.DeptList = db.Dept;
return View();
}
//添加员工
public ActionResult AddEmp()
{
Employee emp = new Employee()
{
DeptId = int.Parse(Request["DeptId"]),
EmpName = Request["txtName"],
EmpPhone =Request["txtPhone"],
EmpArea = Request["txtArea"],
EmpSalary =decimal.Parse(Request["txtSalary"])
};
db.Employee.Add(emp);
int count = db.SaveChanges();
if (count>0)
{
return Content("");
}
else
{
return Content("");
}
}
在AddEmpForm上右键添加视图:
添加页面
//编辑员工页面
public ActionResult EditEmpForm()
{
//查询部门列表
ViewBag.DeptList = db.Dept;
//查询要编辑的列
ViewBag.emp = db.Employee.Find(int.Parse(Request["EmpId"]));
return View();
}
//修改员工
public ActionResult EditEmp()
{
int EmpId = int.Parse(Request["EmpId"]);
Employee emp = db.Employee.Find(EmpId);
emp.DeptId = int.Parse(Request["DeptId"]);
emp.EmpName = Request["txtName"];
emp.EmpPhone = Request["txtPhone"];
emp.EmpArea = Request["txtArea"];
emp.EmpSalary = decimal.Parse(Request["txtSalary"]);
int count=db.SaveChanges();
if (count > 0)
{
return Content("");
}
else
{
return Content("");
}
}
在 EditEmpForm上右键添加视图:
修改页面
//删除员工
public ActionResult DeleteEmp()
{
int EmpId = int.Parse(Request["EmpId"]);
Employee emp = db.Employee.Find(EmpId);
db.Employee.Remove(emp);
int count = db.SaveChanges();
if (count > 0)
{
return Content("");
}
else
{
return Content("");
}
}
删除没有前端页面在Index页面中添加
删除。