asp.net MVC 如何仅修改一列



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

首页控制器.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")
}

尝试使用绑定,但列仍然变为 NULL(以公司名称、地址 1 为例(

[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);

您从数据库获得原始公司,向其提供新值,最后编辑作为参数提供的对象,该参数具有所有其他属性 null。

将其替换为:

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

在根本不应该触及的值上,在view中,在class="form-control"上添加一个disabled属性。喜欢

class="form-control" disabled

另外,为了防止NULL设置值,请在控制器的[HttpPost]部分放置一个Bind()。这样

[HttpPost]
public ActionResult Edit([Bind(Include ="Id,Account_Id,NextField,OtherField,etc.")] IPR_CompanyGen_200200501 CompanyToEdit)

[Bind("")]中,只需输入模型中存在的列名即可。在控制器有if(ModelState.IsValid)

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([Bind(Include="Id,Company_Id,Account_Id,Other_Properties")] 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.Company_ID = CompanyToEdit.Company_ID;
OriginalCompany.WhatEverProperty = CompanyToEdit.WhatEverProperty;
OriginalCompany.Account_ID = CompanyToEdit.Account_ID;
//do this for every property and place a breakpoint to view every value

_db.Entry(OriginalCompany).CurrentValues.SetValues(CompanyToEdit).First();
_db.SaveChanges();
return RedirectToAction("Index");
}
return View();
}

最新更新