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();
}
}
}
MVC CRUD 的两种方法的更多相关文章
- ASP.NET MVC 实现AJAX跨域请求的两种方法
通常发送AJAX请求都是在本域内完成的,也就是向本域内的某个URL发送请求,完成部分页面的刷新.但有的时候需要向其它域发送AJAX请求,完成数据的加载,例如Google. 在ASP.NET MVC 框 ...
- .NET MVC中登录过滤器拦截的两种方法
今天给大家介绍两种ASP中过滤器拦截的两种方法. 一种是EF 的HtppModule,另一种则是灵活很多针对MVC的特性类 Attribute 具体什么是特性类可以参考着篇文章:https://www ...
- C# web api 返回类型设置为json的两种方法
每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...
- windows下获取IP地址的两种方法
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- android 之 启动画面的两种方法
现在,当我们打开任意的一个app时,其中的大部分都会显示一个启动界面,展示本公司的logo和当前的版本,有的则直接把广告放到了上面.启动画面的可以分为两种设置方式:一种是两个Activity实现,和一 ...
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- php如何防止图片盗用/盗链的两种方法(转)
图片防盗链有什么用? 防止其它网站盗用你的图片,浪费你宝贵的流量.本文章向大家介绍php防止图片盗用/盗链的两种方法 Apache图片重定向方法 设置images目录不充许http访问 Apache服 ...
- WPF程序将DLL嵌入到EXE的两种方法
WPF程序将DLL嵌入到EXE的两种方法 这一篇可以看作是<Visual Studio 版本转换工具WPF版开源了>的续,关于<Visual Studio 版本转换工具WPF版开源了 ...
- MongoDB实现分页(两种方法)
1.插入实验数据 偷懒用下samus,100条. ; i < ; i++) { Document doc = new Document(); doc["ID"] = i; d ...
随机推荐
- Qt 学习: 视图选择 (QItemSelectionModel)
博主QQ:1356438802 选择是视图中经常使用的一个操作.在列表.树或者表格中,通过鼠标点击能够选中某一项,被选中项会变成高亮或者反色.在 Qt 中,选择也是使用了一种模型.在 model/vi ...
- 【浅墨Unity3D Shader编程】之中的一个 夏威夷篇:游戏场景的创建 & 第一个Shader的书写
本系列文章由@浅墨_毛星云 出品.转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/40723789 作者:毛星云(浅墨) ...
- java中抽象类和空的方法体有什么区别?
public abstract void test();抽象方法public void test(){};方法体为空这两个有什么区别? public abstract void test(); 抽象方 ...
- Java多线程编程— 概念以及经常使用控制
多线程能满足程序猿编写很有效率的程序来达到充分利用CPU的目的,由于CPU的空暇时间可以保持在最低限度.有效利用多线程的关键是理解程序是并发运行而不是串行运行的.比如:程序中有两个子系统须要并发运行, ...
- Identifying a distributed denial of service (DDOS) attack within a network and defending against such an attack
The invention provides methods, apparatus and systems for detecting distributed denial of service (D ...
- erlang中如何调试程序
学习一门语言,当学习那些基本语法的时候,我们常常会忽略它的程序调试,当程序稍微复杂一点的时候,我们不能保证程序的完全正确,我们会为其发愁,这时,程序的调试就变得相当重要了. 在erlang环境搭 ...
- HDU 1244 Max Sum Plus Plus Plus - dp
传送门 题目大意: 给一个序列,要求将序列分成m段,从左至右每一段分别长l1,l2,...lm,求最大的和是多少. 题目分析: 和最大m段子段和相似,先枚举\(i \in [1,m]\),然后$j \ ...
- Spring MVC的RequestContextHolder使用误区 good
JShop简介:jshop是一套使用Java语言开发的B2C网店系统,致力于为个人和中小企业提供免费.好用的网店系统. 项目主页:http://git.oschina.net/dinguangx/js ...
- 【Codeforces Round #438 A】Bark to Unlock
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 枚举它是在连接处,还是就是整个字符串就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc+ ...
- yii2 实现无极限分类
在商城项目或者其他的项目中无极限分类是非常常见的场景 那么许多人都会这样干 利用递归每次查询数据库,说来惭愧,我以前也是这样干的 总有点误人子弟的感觉 这样做在数据量大的情况下会有一定的延迟 publ ...