gpt4 book ai didi

asp.net MVC如何仅修改一列

转载 作者:行者123 更新时间:2023-12-02 02:51:13 26 4
gpt4 key购买 nike

嗨,我有这段代码可以在将数据更新到数据库时起作用。但是我的表中有 20 列。我怎样才能确定:1) 只有ACCOUNT_ID会被更新吗? (目前更新后所有 17 列都被覆盖为 NULL)2)如何在UI中指示只有ACCOUNT_ID可编辑? (例如变灰)

HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MovieApp.Models;



namespace MovieApp.Controllers
{
public class HomeController : Controller
{
private dtsdbEntities _db = new dtsdbEntities();
// GET: Home
public ActionResult Index()
{
return View(_db.IPR_CompanyGen_200200501.ToList());
}
// GET: Home/Edit/5
public ActionResult Edit(int id)
{
var CompanyToEdit = (from m in _db.IPR_CompanyGen_200200501 where
(m.CompanyID.Equals(id.ToString())) select m);
return View(CompanyToEdit);
}

// GET: Home/Edit/5
[HttpPost]
public ActionResult Edit(IPR_CompanyGen_200200501 CompanyToEdit)
{
var OriginalCompany = (from m in _db.IPR_CompanyGen_200200501 where
(m.CompanyID.Equals(CompanyToEdit.CompanyID.ToString())) select m.First());

_db.Entry(OriginalCompany).CurrentValues.SetValues(CompanyToEdit).First();
_db.SaveChanges();

return RedirectToAction("Index");
}





}

internal class MoviesDBEntities
{
}

}

编辑.cshtml

       @model MovieApp.Models.IPR_CompanyGen_200200501

@{
ViewBag.Title = "Edit";
}

<h2>Edit</h2>


@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>IPR_CompanyGen_200200501</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.CompanyID)

<div class="form-group">
@Html.LabelFor(model => model.CompanyName, htmlAttributes: new { @class = "control-label
col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CompanyName, new { htmlAttributes = new { @class =
"form-control" } })
@Html.ValidationMessageFor(model => model.CompanyName, "", new { @class = "text-
danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.ACCOUNT_ID, htmlAttributes: new { @class = "control-label
col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ACCOUNT_ID, new { htmlAttributes = new { @class =
"form-control" } })
@Html.ValidationMessageFor(model => model.ACCOUNT_ID, "", new { @class = "text-
danger" })
</div>
</div>


<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

尝试使用 Bind 但列仍然为 NULL(以 CompanyName 、 Address1 为例)

[HttpPost]
public ActionResult Edit([Bind(Include = "CompanyID,CompanyName,Address1,Address2,Address3,City,StateProvinceCode,PostalCode,CountryCode,Region,PhoneNumber,URL,PrimaryCompanyType, DominantCompanyStyle, DominantCompanyOrientation, Buy_side, Sell_side, Strategic, EqPortfolioTurnover, ReportedEquityAssets, Status, LastModifiedDate, ACCOUNT_ID, CRM_Comments")] IPR_CompanyGen_200200501 CompanyToEdit)
{

if (ModelState.IsValid) {
var OriginalCompany = (from m in _db.IPR_CompanyGen_200200501 where (m.CompanyID.Equals(CompanyToEdit.CompanyID.ToString())) select m).First();


OriginalCompany.CompanyName = CompanyToEdit.CompanyName;
OriginalCompany.Address1 = CompanyToEdit.Address1;


_db.Entry(OriginalCompany).CurrentValues.SetValues(CompanyToEdit);

_db.SaveChanges();

return RedirectToAction("Index");
}
return View();
}

最佳答案

这是您的代码的问题:

 OriginalCompany.CompanyName = CompanyToEdit.CompanyName;
OriginalCompany.Address1 = CompanyToEdit.Address1;


_db.Entry(OriginalCompany).CurrentValues.SetValues(CompanyToEdit);

您从数据库中获取originalCompany,为其分配新值,最后编辑作为参数提供的对象,该对象的所有其他属性均为空。

将其替换为:

_db.Entry(OriginalCompany).CurrentValues.SetValues(OriginalCompany);

关于asp.net MVC如何仅修改一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61940665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com