//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();
        }
    }
}

最新文章

  1. VBS实现定时发送邮件
  2. Mac平台下启动MySQL到完全终止MySQL----终端八步走
  3. ios专题 - 斯坦福大学iOS开发公开课总结
  4. mysql存储过程写法—动态参数运用
  5. Quartz2D使用
  6. phoneGap开发环境搭建(android)
  7. python自学笔记(八)python语句
  8. AspNetWebApi管线中如果定义两种类型的消息处理程序(全局/路由)
  9. [Android学习笔记4]四大应用组件之一:Service 上
  10. git全部使用步骤
  11. 移植python笔记
  12. App内切换语言
  13. POJ 2449 Dijstra + A* K短路
  14. 学习Matplotlib
  15. etcd集群部署
  16. Web Service进阶(五)SOAPBinding方式讲解
  17. Linux的启动流程(一)
  18. 十大经典排序算法的python实现
  19. Android之电话拨号和短信
  20. global

热门文章

  1. Mysql 安装(Using Generic Binaries)
  2. Ajax方法
  3. [Angular] Observable.catch error handling in Angular
  4. php实现求一个数的质数因子
  5. [转] Valgrind使用
  6. ios9 xcode7以后编译需要进行的几项设置
  7. 语言的学习 —— 西班牙语(español)
  8. selenium 爬取空间说说
  9. hadoop 3.x 回收站
  10. bootstrap paginator使用简述