MVC初体验-EF系列(状态跟踪的修改)(21)

2021-04-19 16:28

阅读:584

标签:公司   http   rabl   state   out   att   body   index   doctype   

主要就是在没有AddOrUpdate方法的时候,你想要改变数据库内的一条数据如何操作

核心操作(这里主要展示下如何只改变一个字段“ShipName”的值)

(改变整条记录的值在Attach后面操作db.Entry(order).State = EntityState.Modified;然后savechange即可,但是注意这样操作是将新建的order对象对应的字段全部替换了)

[HttpPost]
        public ActionResult Edit()
        {
            int orderID = int.Parse(HttpContext.Request["orderID"]);
            string shipName = HttpContext.Request["shipname"];
            Orders order = new Orders { OrderID = orderID,ShipName = shipName };
            db.Set().Attach(order);
            db.Entry(order).Property("ShipName").IsModified = true;
            db.Entry(order).Property("ShipName").CurrentValue = shipName;
            db.SaveChanges();

            return Redirect("/Orders/Edit/?id="+orderID);
        }

 

后台代码:

技术图片技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using T1_EF.Models;

namespace T1_EF.Controllers
{
    public class OrdersController : Controller
    {
        DbContext db = new NorthwindEntities();
        // GET: Orders
        public ActionResult Index()
        {
            var list = from order in db.Set() select order;
            ViewData.Model = list;
            return View();
        }
        public ActionResult Edit(int id)
        {
            var data = db.Set().Where(o=>o.OrderID==id).FirstOrDefault();
            ViewData.Model = data;
            return View();
        }
        [HttpPost]
        public ActionResult Edit()
        {
            int orderID = int.Parse(HttpContext.Request["orderID"]);
            string shipName = HttpContext.Request["shipname"];
            Orders order = new Orders { OrderID = orderID,ShipName = shipName };
            db.Set().Attach(order);
            db.Entry(order).Property("ShipName").IsModified = true;
            db.Entry(order).Property("ShipName").CurrentValue = shipName;
            db.SaveChanges();

            return Redirect("/Orders/Edit/?id="+orderID);
        }
    }
}
View Code

前台代码Index.cshtml:

技术图片技术图片
@model IEnumerableT1_EF.Models.Orders>
@{
    Layout = null;
}

DOCTYPE html>

html>
head>
    meta name="viewport" content="width=device-width" />
    title>Indextitle>
head>
body>
    div> 
        table border="1">
            tr>
                td>订单编号td>
                td>客户名称td>
                td>船舶公司td>
                td>船名td>
                td>修改td>
            tr>
            @foreach (var item in Model)
            {
            tr>
                td>@item.OrderIDtd>
                td>@item.Customers.CompanyNametd>
                td>@item.Shippers.CompanyNametd>
                td>@item.ShipNametd>
                td>a href="@Url.Action("Edit","Orders")?id=@item.OrderID">修改a>td>
            tr>
            }
        table>
    div>
body>
html>
View Code

Edit.cshtml:

技术图片技术图片
@model T1_EF.Models.Orders
@{
    Layout = null;
}

DOCTYPE html>

html>
head>
    meta name="viewport" content="width=device-width" />
    title>Edittitle>
head>
body>
    div> 
        table border="1">
            tr>
                td>@Model.OrderIDtd>
                td>@Model.Customers.CompanyNametd>
                td>@Model.Shippers.CompanyNametd>
                td>@Model.ShipNametd>
            tr>
        table>
        form action="@Url.Action("Edit","Orders")" method="post">
            input type="hidden" name="orderID" value="@Model.OrderID"/>
            input type="text" name="shipname" placeholder="修改ShipName的值" />
            input type="submit" name="change" value="修改ShipName" />
        form>
    div>
body>
html>
View Code

 

 

End

MVC初体验-EF系列(状态跟踪的修改)(21)

标签:公司   http   rabl   state   out   att   body   index   doctype   

原文地址:https://www.cnblogs.com/LeeSki/p/12267030.html


评论


亲,登录后才可以留言!