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


原文地址:https://www.cnblogs.com/dxmfans/p/9434740.html