Hi I have this piece of code that works when updating data into db. However i have 20 columns in my table. how can i make sure: 1) Only ACCOUNT_ID will be updated? (currently all 17 columns get overwritten into NULL after updating) 2) how to indicate in UI that only ACCOUNT_ID is editable? (eg greyed out)
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
{
}
}
Edit.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")
}
Tried with Bind but still columns goes to NULL (take CompanyName , Address1 as example)
[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();
}