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的更多相关文章
随机推荐
- Ejabberd源码解析前奏--集群
一.如何工作 一个XMPP域是由一个或多个ejabberd节点伺服的. 这些节点可能运行在通过网络连接的不同机器上. 它们都必须有能力连接到所有其它节点的4369端口, 并且必须有相同的 magic ...
- gdi写的2048
//-------------------------------------------[头文件及引用]----------------------------------------------- ...
- linux 查看用户所在组(groups指令的使用) 含实例
经常将某个文件夹的权限赋给某个用户的时候,也需要配置该用户所在的组,因此,我们需要查看该用户有哪些组,我们可以使用如上命令查看用户所在组 [oracle@gl ~]$ vi /etc/group ro ...
- Ofbiz初探
转:http://xmmartin.blog.51cto.com/2310947/771236 主导建设一个电子商务系统希望从Ofbiz了解中获得一些借鉴1.下载ofbiz,目前的版本是10.04,下 ...
- Selenium 使用方法小结
基本介绍: Selenium工具专门为WEB应用程序编写的一个验收测试工具. Selenium的核心:browser bot,是用JAVASCRIPT编写的. Selenium工具有4种:Sele ...
- Android学习笔记:TabHost 和 FragmentTabHost
TabHost 命名空间: android.widget.TabHost 初始化函数(必须在addTab之前调用): setup(); 包含两个子元素: 1.Tab标签容器TabWidget(@and ...
- #ifdef _cplusplus (转)
原文不可考,转载链接:http://blog.csdn.net/owldestiny/article/details/5772916 有发现原文的请告知,我会及时更新. 时常在cpp的代码之中看到这样 ...
- 深入浅出JavaScript函数 v 0.5
本文的观点是建立在<JavaScript权威指南 6th Ed> <JavaScript高级编程 3th Ed> <JavaScript精粹 2th Ed>之上, ...
- android中少用静态变量(android静态变量static生命周期)
在android中,要少用静态变量. 我现在做的一个应用中,之前的开发人员使用静态变量来存储cookie,这个全局的静态变量用来验证身份. 这时客户反应,应用长时间不使用,再次使用,会提示身份过期. ...
- A Tour of Go Exercise: Maps
Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Tes ...