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的更多相关文章
随机推荐
- 17、手势(Gesture)
课程目标: 学习Android必不可少的手势的功能 了解手势识别原理 , 掌握制作,加载以及识别手势 写出自己的手势Demo 重点难点:手势机制的了解 手势库的制作 考核目标:请说一下手势库的 ...
- 点亮一个led
1:RS232电平:计算机串口 高电平为-12v,低电平为+12v,所以计算机与单片机进行通信的时候需要加电平转换芯片max232(高电平为-9到-12,低电平为+3到+12之间的. max232通常 ...
- Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution
class Solution { public: int strStr(char *haystack, char *needle) { , skip[]; char *str = haystack, ...
- cocos2d-x编译到android平台后,增加返回键和菜单键支持
在头文件中增加函数 virtual void keyBackClicked();//android返回键 virtual void keyMenuClicked();//android菜单键 在ini ...
- 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇05:二段跳》
5.二段跳 二段跳概述: 基本跑酷游戏的框架搭建完毕,开发者会根据开发的游戏特性,增设一些额外功能,使游戏具有可玩性性和画面感.下面我们以角色的二段跳为例,来了解在跑酷游戏中增设其它功能的流程.二段跳 ...
- Bzoj-2705 Longge的问题 欧拉函数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2705 题意: 求 sigma(gcd(i,n), 1<=i<=n<2^3 ...
- Android实例-录音与回放(播放MP3)(XE8+小米2)
结果: 1.增加ActionList中的Action时,需要跳到Master界面,不能在Android4Phonel界面下. 2.如果不打开权限的话,会提示“该设备不支持停止录音操作”(Record ...
- Oracle-查看oracle是否有表被锁
问题现象: 查看oracle是否有表被锁 解决方法: select sid,serial#,program,terminal,username,b.object_id,c.object_name f ...
- JavaScript一道面试题求y的值是? z 的值是? s的值是?
原文:http://www.zhufengpeixun.cn/JavaScriptmianshiti/2014-04-01/287.html < script type = "text ...
- 理解js中的原型链,prototype与__proto__的关系
说到prototype,就不得不先说下new的过程. 我们先看看这样一段代码: 1 <script type="text/javascript"> 2 var Pers ...