zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }


  • 相关阅读:
    Membership provider Role provider 机制详解
    android Toast大全(五种情形)建立属于你自己的Toast
    android:scaleType属性
    Android接收短信同时获取短信内容
    JAVA三大框架的各自作用
    Android短信监听器
    ImageView / ImageButton 图片太大或者太小解决方法
    Android LayoutInflater详解
    Android开发之Intent.Action
    Android实现短信监听并且转发到指定的手机号,转发后不留痕
  • 原文地址:https://www.cnblogs.com/dxmfans/p/9434740.html
Copyright © 2011-2022 走看看