using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iFlytekDemo.Models; namespace iFlytekDemo.Controllers
{
public class CitiesController : Controller
{
private readonly ICityRepository cityRepository; // If you are using Dependency Injection, you can delete the following constructor
public CitiesController() : this(new CityRepository())
{
} public CitiesController(ICityRepository cityRepository)
{
this.cityRepository = cityRepository;
} //
// GET: /Cities/ public ViewResult Index()
{
return View(cityRepository.AllIncluding(city => city.Employees));
} //
// GET: /Cities/Details/5 public ViewResult Details(int id)
{
return View(cityRepository.Find(id));
} //
// GET: /Cities/Create public ActionResult Create()
{
return View();
} //
// POST: /Cities/Create [HttpPost]
public ActionResult Create(City city)
{
if (ModelState.IsValid) {
cityRepository.InsertOrUpdate(city);
cityRepository.Save();
return RedirectToAction("Index");
} else {
return View();
}
} //
// GET: /Cities/Edit/5 public ActionResult Edit(int id)
{
return View(cityRepository.Find(id));
} //
// POST: /Cities/Edit/5 [HttpPost]
public ActionResult Edit(City city)
{
if (ModelState.IsValid) {
cityRepository.InsertOrUpdate(city);
cityRepository.Save();
return RedirectToAction("Index");
} else {
return View();
}
} //
// GET: /Cities/Delete/5 public ActionResult Delete(int id)
{
return View(cityRepository.Find(id));
} //
// POST: /Cities/Delete/5 [HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
cityRepository.Delete(id);
cityRepository.Save(); return RedirectToAction("Index");
} protected override void Dispose(bool disposing)
{
if (disposing) {
cityRepository.Dispose();
}
base.Dispose(disposing);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iFlytekDemo.Models; namespace iFlytekDemo.Controllers
{
public class EmployeesController : Controller
{
private readonly ICityRepository cityRepository;
private readonly IEmployeeRepository employeeRepository; // If you are using Dependency Injection, you can delete the following constructor
public EmployeesController() : this(new CityRepository(), new EmployeeRepository())
{
} public EmployeesController(ICityRepository cityRepository, IEmployeeRepository employeeRepository)
{
this.cityRepository = cityRepository;
this.employeeRepository = employeeRepository;
} //
// GET: /Employees/ public ViewResult Index()
{
return View(employeeRepository.AllIncluding(employee => employee.City));
} //
// GET: /Employees/Details/5 public ViewResult Details(int id)
{
return View(employeeRepository.Find(id));
} //
// GET: /Employees/Create public ActionResult Create()
{
ViewBag.PossibleCities = cityRepository.All;
return View();
} //
// POST: /Employees/Create [HttpPost]
public ActionResult Create(Employee employee)
{
if (ModelState.IsValid) {
employeeRepository.InsertOrUpdate(employee);
employeeRepository.Save();
return RedirectToAction("Index");
} else {
ViewBag.PossibleCities = cityRepository.All;
return View();
}
} //
// GET: /Employees/Edit/5 public ActionResult Edit(int id)
{
ViewBag.PossibleCities = cityRepository.All;
return View(employeeRepository.Find(id));
} //
// POST: /Employees/Edit/5 [HttpPost]
public ActionResult Edit(Employee employee)
{
if (ModelState.IsValid) {
employeeRepository.InsertOrUpdate(employee);
employeeRepository.Save();
return RedirectToAction("Index");
} else {
ViewBag.PossibleCities = cityRepository.All;
return View();
}
} //
// GET: /Employees/Delete/5 public ActionResult Delete(int id)
{
return View(employeeRepository.Find(id));
} //
// POST: /Employees/Delete/5 [HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
employeeRepository.Delete(id);
employeeRepository.Save(); return RedirectToAction("Index");
} protected override void Dispose(bool disposing)
{
if (disposing) {
cityRepository.Dispose();
employeeRepository.Dispose();
}
base.Dispose(disposing);
}
}
}

C#_MVC_Repository_CURD_Controller的更多相关文章

随机推荐

  1. Windows Server 2008文件同步

    配置Windows Server 2008文件同步   摘要: 众所周知,Linux系统可以用rsync来实现文件或目录的同步,windows系统下也一样可以.我们现在就用cwRsync来实现wind ...

  2. 【转】 当程序崩溃的时候怎么办 Part-2

    转自:http://www.tairan.com/archives/1143 欢迎回到当程序崩溃的时候怎么办 教程! 在这个教程的第一部分,我们介绍了SIGABRT和EXC_BAD_ACCESS错误, ...

  3. CF GYM 100703A Tea-drinking

    题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...

  4. shell获取db信息及上传下载操作

    这个脚本是获取目标机器的db信息和os信息的.os信息很好获取,db的信息包含db名字,db版本以及所有的db instance,db信息的获取稍显复杂,下面是整个脚本代码: 关键字: awk, se ...

  5. 绕过CDN查找网站真实IP方法

    查找网站 源IP方法: 如果遇到需要绕过CDN,查找网站真实IP地址时,可以采用如下方法: 假设主站服务和邮件服务在同一台服务器: 1.在网站用QQ邮箱注册账号: 2.收取注册验证邮件: 3.查看邮件 ...

  6. [原创]使用squish打包与混淆cocos2d-x的lua脚本

    squish是一个开源的用于打包lua脚本的小工具,它的主要功能是将多个lua文件整合成一个文件,并在此基础上做压缩和混淆等处理,混淆和压缩后的代码可以直接被执行而不需要先做解压还原等操作. 它的gi ...

  7. 【译】 AWK教程指南 10编写可与用户交互的AWK程序

    执行awk程序时,awk会自动从文件中读取数据来进行处理,直到文件结束.只要将awk读取数据的来源改成键盘输入,便可设计与awk 交互的程序.本节将提供一个该类程序的范例. 范例:本节将编写一个英语生 ...

  8. 机器学习-----线性回归浅谈(Linear Regression)

    Linear Regreesion          在现实生活中普遍存在着变量之间的关系,有确定的和非确定的.确定关系指的是变量之间可以使用函数关系式表示,还有一种是属于非确定的(相关),比如人的身 ...

  9. leetcode@ [72/115] Edit Distance & Distinct Subsequences (Dynamic Programming)

    https://leetcode.com/problems/edit-distance/ Given two words word1 and word2, find the minimum numbe ...

  10. Kooboo中如何切换数据库(注意:如果切换数据库,需要Kooboo中没有一个website 否则会报错数据库中没有表之类的)

    Setup database provider 来自Kooboo document   Kooboo CMS can almost support all the types of database, ...