I have the following code that works fine if I pass all the values of a “Page” back from my view. My problem is that in this view I only have two of the database fields that I want to update.
This code updates the two fields from my view fine but sets the rest back to NULL. What is the best way to get around this?
[HttpPost]
public ActionResult Page(Page page)
{
if (ModelState.IsValid)
{
db.Pages.Attach(page);
db.ObjectStateManager.ChangeObjectState(page, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(page);
}
bojangles
Participant
974 Points
642 Posts
Submitting a partial Modal
May 26, 2012 09:45 AM|LINK
Hi,
I have the following code that works fine if I pass all the values of a “Page” back from my view. My problem is that in this view I only have two of the database fields that I want to update.
This code updates the two fields from my view fine but sets the rest back to NULL. What is the best way to get around this?
[HttpPost] public ActionResult Page(Page page) { if (ModelState.IsValid) { db.Pages.Attach(page); db.ObjectStateManager.ChangeObjectState(page, EntityState.Modified); db.SaveChanges(); return RedirectToAction("Index"); } return View(page); }Cheers,
Mike.
CPrakash82
All-Star
18296 Points
2847 Posts
Re: Submitting a partial Modal
May 26, 2012 10:53 AM|LINK
can you try something like this.
db.Pages.Attach(page);
var entry = db.ObjectStateManager.GetObjectStateEntry(page);
entry.SetModifiedProperty("[Name]");
db.SaveChanges();
Thanks,