MVC CRUD 的两种方法
//Index.cshtml
@model IQueryable<MvcExam2.Models.Product>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@Html.ActionLink("添加", "Add", "ProductCrud")
</div>
<div>
<table>
<tr>
<th>ModelNumber</th>
<th>ModelName</th>
<th>UnitCost</th>
<th>修改</th>
<th>删除</th>
</tr>
@foreach(MvcExam2.Models.Product p in Model)
{
<tr>
<td>@p.ModelNumber</td>
<td>@p.ModelName</td>
<td>@p.UnitCost</td>
<td>@Html.ActionLink("修改", "Update", "ProductCrud",new RouteValueDictionary(new { id = @p.ProductID }),null)</td>
<td>@Html.ActionLink("删除","Delete","ProductCrud", new RouteValueDictionary(new { id = @p.ProductID }), null)</td>
</tr>
}
</table>
</div>
</body>
</html>
//Add.cshtml
@model MvcExam2.Models.Product
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Add</title>
</head>
<body>
<div>
@using (Html.BeginForm("Add", "ProductCrud", FormMethod.Post))
{
<span>ModelNumber:</span>@Html.TextBoxFor(p => p.ModelNumber);<br />
<span>ModelName:</span>@Html.TextBoxFor(p => p.ModelName);<br />
<span>UnitCost:</span>@Html.TextBoxFor(p=>p.UnitCost);<br />
<input type="submit" name="name" value="submit" />
}
</div>
</body>
</html>
//Update.cshtml
@model MvcExam2.Models.Product
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Update</title>
</head>
<body>
<div>
@using (Html.BeginForm("Update", "ProductCrud", FormMethod.Post))
{
@Html.HiddenFor(p=>p.ProductID)
<span>ModelNumber:</span>@Html.TextBoxFor(p => p.ModelNumber) <br />
<span>ModelName:</span>@Html.TextBoxFor(p => p.ModelName) <br />
<span>UnitCost:</span>@Html.TextBoxFor(p => p.UnitCost) <br />
<input type="submit" name="name" value="submit" />
}
</div>
</body>
</html>
//ProductCrudController
using MvcExam2.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity.Migrations;
namespace MvcExam2.Controllers
{
public class ProductCrudController : Controller
{
DbContext context = new StoreContext();
// GET: ProductCrud
public ActionResult Index()
{
IQueryable<Product> list = context.Set<Product>();
return View(list);
}
public ActionResult Add()
{
return View();
}
[HttpPost]
public ActionResult Add(Product p)
{
p.CategoryID = 14;
//第一种方法:用curd方法
//context.Set<Product>().Add(p);
//第二种方法:用状态跟踪
context.Set<Product>().Attach(p);
context.Entry(p).State = EntityState.Added;
int result = context.SaveChanges();
if (result>0)
{
return Redirect(Url.Action("Index"));
}
else
{
return Redirect(Url.Action("Error"));
}
}
public ActionResult Update(int id)
{
ViewData.Model= context.Set<Product>().Where(p => p.ProductID == id).FirstOrDefault();
return View();
}
[HttpPost]
public ActionResult Update(Product p)
{
p.CategoryID = 14;
//第一种方法:用curd方法
// context.Set<Product>().AddOrUpdate(p);//需要引用System.Data.Entity.Migrations;
//第二种方法:用状态跟踪
context.Set<Product>().Attach(p);
context.Entry(p).State = EntityState.Modified;
int result = context.SaveChanges();
if (result > 0)
{
return Redirect(Url.Action("Index"));
}
else
{
return Redirect(Url.Action("Error"));
}
}
public ActionResult Delete(int id)
{
var product= context.Set<Product>().Where(p => p.ProductID == id).FirstOrDefault();
//第一种方法:用curd方法
//context.Set<Product>().Remove(obj);
//第二种方法:用状态跟踪
context.Set<Product>().Attach(product);
context.Entry(product).State = EntityState.Deleted;
int result = context.SaveChanges();
if (result > 0)
{
return Redirect(Url.Action("Index"));
}
else
{
return Redirect(Url.Action("Error"));
}
}
public ActionResult Error()
{
return View();
}
}
}
最新文章
- VBS实现定时发送邮件
- Mac平台下启动MySQL到完全终止MySQL----终端八步走
- ios专题 - 斯坦福大学iOS开发公开课总结
- mysql存储过程写法—动态参数运用
- Quartz2D使用
- phoneGap开发环境搭建(android)
- python自学笔记(八)python语句
- AspNetWebApi管线中如果定义两种类型的消息处理程序(全局/路由)
- [Android学习笔记4]四大应用组件之一:Service 上
- git全部使用步骤
- 移植python笔记
- App内切换语言
- POJ 2449 Dijstra + A* K短路
- 学习Matplotlib
- etcd集群部署
- Web Service进阶(五)SOAPBinding方式讲解
- Linux的启动流程(一)
- 十大经典排序算法的python实现
- Android之电话拨号和短信
- global