MVC初体验-EF系列(状态跟踪的修改)(21)
2021-04-19 16:28
标签:公司 http rabl state out att body index doctype 主要就是在没有AddOrUpdate方法的时候,你想要改变数据库内的一条数据如何操作 核心操作(这里主要展示下如何只改变一个字段“ShipName”的值) (改变整条记录的值在Attach后面操作db.Entry(order).State = EntityState.Modified;然后savechange即可,但是注意这样操作是将新建的order对象对应的字段全部替换了) 后台代码: 前台代码Index.cshtml: Edit.cshtml: End MVC初体验-EF系列(状态跟踪的修改)(21) 标签:公司 http rabl state out att body index doctype 原文地址:https://www.cnblogs.com/LeeSki/p/12267030.html[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
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
@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>
@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>
文章标题:MVC初体验-EF系列(状态跟踪的修改)(21)
文章链接:http://soscw.com/index.php/essay/76740.html