C#_MVC_Repository_CURD_Controller
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的更多相关文章
随机推荐
- 陈正冲老师对于c语言野指针的解释
那到底什么是野指针呢?怎么去理解这个“野”呢?我们先看别的两个关于“野”的词: 野孩子:没人要,没人管的孩子:行为动作不守规矩,调皮捣蛋的孩子.野狗:没有主人的狗,没有链子锁着的狗,喜欢四处咬人. 对 ...
- (4)java方法区
java方法区[名词解析] --->和java堆一样,方法区是一块所有线程共享的内存区域. --->保存系统的类信息,比如,类的字段,方法,常量池等. ...
- Oracle 给表添加主键和使ID自增、触发器、创建结构一样的表
1.关于主键:在建表时指定primary key字句即可:create table test( id number(6) primary key, name varchar2(30));如果是对于已经 ...
- Spring @Resource、@Autowired、@Qualifier的注解注入及区别
spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...
- java.lang.ExceptionInInitializerError的原因(转)
这个错误是说变量初始化出现问题,通常出现在静态变量尤其是单例模式.这种问题往往是初始化顺序不对造成的,下面举个简单的例子. import java.util.HashMap; import java. ...
- 树莓pi快速ssh【校园网】
校园网是NAT后的,没有显示器,我把路由器的DHCP服务关了,这样的情况下怎么ssh到树莓pi? 把树莓pi 连到路由器上(已经关闭DHCP),手机安装FING https://play.google ...
- VS2010手动添加外部工具和快捷键
一.添加外部工具 1.选择工具->外部工具 2.选择添加,然后填相应的程序路径和传给程序的参数. 标题:所创建的工具名字 命令:工具路径 参数:工具命令行参数 初始目录:工具工作目录,有些工具可 ...
- PHP与MySQL动态网站开发2
处理HTML表单 <input type="text" name="city"/> 通过$_REQUEST['city'];访问,$_REQUEST ...
- 宿主进程 vshost.exe
Hosting Process (vshost.exe) 宿主进程是VS的一个特性.可以提高调试的性能,可以进行部分信任调试(partial trust debugging),可以进行设计时表达式计算 ...
- Java 8 开发顶级技巧
本文由码农网 – 小峰原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 我使用Java 8编码已经有些年头,既用于新的应用程序,也用来迁移现有的应用,感觉是时候写一些我发现的非常有用的 ...