Click here to Skip to main content
15,887,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I update single field in the database without updating the other fields using MVC?

The sample code blew, I want to update only the field "PROJECTINITIALAPPROVALSTATUS" but it update the other fields with null values.

SQL
[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult SuggestedProjectApproval([Bind(Include = "RECID,PROJECTNO,PROJECTDESC,PROJECTTEAMLEADERID,PROJECTYEAR,PROJECTINITIALAPPROVALSTATUS,PROJECTINITIALAPPROVALNOTES")] STUDY_PROJECTS studyProject, string Approve, string Refuse)
    {
        if (ModelState.IsValid)
        {
             db.Entry(studyProject).State = EntityState.Modified;
            if (!string.IsNullOrEmpty(Approve))
            {
                studyProject.PROJECTINITIALAPPROVALSTATUS = 1;
            }
            else if (!string.IsNullOrEmpty(Refuse))
            {

                studyProject.PROJECTINITIALAPPROVALSTATUS = 2;
            }

            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(studyProject);
    }
Posted
Comments
mr.priyank 24-Sep-14 6:48am    
Which version of entity framework are you using?

Try this. Set true only for the properties that needs to be updated.

C#
db.Entry(studyProject).Property(e => e.SomeProperty).IsModified = true;
 
Share this answer
 
v2
Take a look on these answers will help you
How to update only one field using Entity Framework?[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900