ella
ella

Reputation: 183

asp.net MVC how to modify one column only

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();
    }

Upvotes: 3

Views: 1952

Answers (2)

Flori Bruci
Flori Bruci

Reputation: 456

This is your problem with your code:

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


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

You get the originalCompany from db assing new values to it and finally you edit the object which came as parameter which have all other properties null.

Replace it with this:

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

Upvotes: 4

jaabh
jaabh

Reputation: 1004

On the values that should not be touched at all, in the view, on the class="form-control" add a disabled attribute. Like

class="form-control" disabled

Also to prevent the values from being set NULL, put a Bind() in the [HttpPost] part of the controller. Like so

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

Inside of the [Bind("")], just put the column names that exist in the model. In the controller have 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();
}

Upvotes: 2

Related Questions