标签:style blog http io ar os sp for 文件
在Controllers文件夹下添加 一个控制器 HomeController.cs。我这里之所以说的简单,并没有讲一些理念性的东西,是为了尽快让大家落实到代码上来。而且这就码字工的一个动作吧,没什么可说的。至于不知道添加的,我相信肯学的人一定能找到路子,比如你没有控制器这个概念,看到我上面一句话,你就会问度娘,c#如何添加控制器。
#region 查
/// <summary>
/// 查
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
var models = from m in db.Provinces select m;
return View(models.ToList());
}
#endregion
#region 增
/// <summary>
/// 增
/// </summary>
/// <returns></returns>
public ActionResult Create()
{
return View();
}
/// <summary>
/// 接收创建页面的数据并存储
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Create(Province model)
{
//
if (ModelState.IsValid)
{
db.Provinces.Add(model);//添加
db.SaveChanges();//提交至数据库
return RedirectToAction("Index");
}
return View(model);
}
#endregion
#region 删
/// <summary>
/// 删
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Delete(int id)
{
var model = db.Provinces.Find(id);
if (model == null)
{
return RedirectToAction("Index");
}
return View(model);
}
/// <summary>
/// 删除,签名一致方法的处理
/// </summary>
/// <param name="id"></param>
/// <param name="collection"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
var model = db.Provinces.Find(id);
if (model != null)
{
db.Provinces.Remove(model);
db.SaveChanges();
}
return RedirectToAction("Index");
}
#endregion
#region 改
/// <summary>
/// 改
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Edit(int id)
{
var model = db.Provinces.Find(id);
//如果为空,直接重定向到index
if (model == null) return RedirectToAction("Index");
return View(model);
}
/// <summary>
/// 接收修改页面的数据并存储
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Edit(Province model)
{
if (ModelState.IsValid)
{
//把存储状态修改成“Modified”
db.Entry(model).State = System.Data.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
#endregion
#region 明细
/// <summary>
/// 明细
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Details(int id)
{
var model = db.Provinces.Find(id);
if (model == null)
{
return RedirectToAction("Index");
}
return View(model);
}
#endregion
标签:style blog http io ar os sp for 文件
原文地址:http://www.cnblogs.com/mahun/p/4116373.html