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. 各种兼容的placeholder

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  2. Android 多种方式正确的加载图像,有效避免oom

    图像加载的方式: Android开发中消耗内存较多一般都是在图像上面,本文就主要介绍怎样正确的展现图像减少对内存的开销,有效的避免oom现象.首先我们知道我的获取图像的来源一般有三种源头:1.从网络加 ...

  3. Gradle使用手册(一):为什么要用Gradle?

    原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Using-sourceCompatibility-1. ...

  4. 问题:如何在固定大小的DIV层插入N多个图片

    这是贴友问的一个问题,具体需求是: 如何在固定大小的DIV层插入N多个图片,使其一行排列,超出层宽时出现滑动条? 原以为利用overflow属性可以实现,但是测试失败.后来利用div层叠实现了效果. ...

  5. EasyUI + Spring MVC + hibernate实现增删改查导入导出

    (这是一个故事--) 前言 作为一个JAVA开发工程师,我觉得最基本是需要懂前端.后台以及数据库. 练习的内容很基础,包括:基本增删改查.模糊查询.分页查询.树菜单.上传下载.tab页 主管发我一个已 ...

  6. SRM 599 DIV 2

    rating又掉了...变灰色了%>_<%.250pt很简单,一眼看上去是个背包,没有多想立马写了个01背包,后面发现其实就是个简单的排序...因为只是需要求数量而已.500pt被我写残了 ...

  7. Java网络编程(TCP客户端)

    TCP传输:两个端点建立连接后会有一个传输数据的通道,这个通道就称为流,而且是建立在网络基础上的流,之为socket流,该流中既可以读取也可以写入. TCP的两个端点:一个客户端:ServerSock ...

  8. Android实例-打电话、发短信和邮件,取得手机IMEI号(XE8+小米2)

    结果: 1.不提示发短信卡住,点击没有反映,我猜想,可能是因为我用的是小米手机吧. 2.接收短信报错,我猜想可能是我改了里面的方法吧(哪位大神了解,求指教). 3.project -->opti ...

  9. Android问题-selection contains a component,button7,introduced in an ancestor and cannot be deleted.

    问题现象: 在开发Android时增加的控件想删除,可是删除时提示“Android问题-selection contains a component,button7,introduced in an ...

  10. hdoj 4325 Flowers【线段树+离散化】

    Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...