How to get old entity value on update in ASP.NET MVC

Carl

I have a simple MVC 5 Application, In my controller I want to know how I can access the current data from database before the db.SaveChanges() is called. I have the following code:

// GET:
public ActionResult Edit(int id)
{
    Product product = db.Products.Find(id);
    return View(product);
}

// POST:
[HttpPost]
public ActionResult Edit([Bind(Include = "id,title,content,price)] Product product)
{
    /* How to access old value from database here? */

    db.Entry(product).State = EntityState.Modified;
    db.SaveChanges();
}

If I do product.title it is retrieving the new value, I want to access the old value before the save.

I tried the accepted answer in this link: How to access old entity value in ASP.NET Entity Framework but it didn't work for me.

Carl

You can do:

// POST:
[HttpPost]
public ActionResult Edit([Bind(Include = "id,title,content,price)] Product product)
{
    var original_data = db.Products.AsNoTracking().Where(P => P.id == product.id).FirstOrDefault();

    /* Use original_data.title here */

    db.Entry(product).State = EntityState.Modified;
    db.SaveChanges();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get an enum value from my ASP.NET MVC property?

From Dev

How to get a session value in cshtml file in ASP.Net MVC4

From Dev

How to get old entity value in @HandleBeforeSave event to determine if a property is changed or not?

From Dev

How to get the value from one table and put into another table using asp.net mvc 4 and jquery

From Dev

How to get an enum value ASP.NET MVC

From Dev

In ASP.NET MVC 4 Razor, how do you get the value (text) of @Html.Label?

From Dev

Using ASP.NET MVC 5, how do I get the localized DisplayAttribute string for an Enum value in a View?

From Dev

How to update a collection inside an entity within a post action in ASP.NET MVC5?

From Dev

How to Update Model in ASP NET MVC 6?

From Dev

Get session value OnConfiguring in asp.net mvc 6 entity framework 7

From Dev

Update a record in ASP .Net MVC

From Dev

How to Get Custom Property Value of the ApplicationUser in the ASP.Net MVC 5 View?

From Dev

How to get a rating value in asp.net mvc4?

From Dev

CreateCriteria and old NHibernate in ASP.NET MVC

From Dev

How to get Checkbox value in MVC asp.net using JQuery

From Dev

How to get the value from one table and put into another table using asp.net mvc 4 and jquery

From Dev

How to create users from old asp.net membership to new MVC .net and send password reset

From Dev

How to get a value from View to JavaScript in asp.net MVC

From Dev

ASP .Net MVC get value from layout

From Dev

Using ASP.NET MVC 5, how do I get the localized DisplayAttribute string for an Enum value in a View?

From Dev

Update a record in ASP .Net MVC

From Dev

update single column of Stub Entity in asp.net mvc

From Dev

How can I get value of EditorFor in ASP.Net Mvc and passing value in a button as parameter

From Dev

How do I get the Id of a user given the email address in ASP.NET MVC Entity Framework

From Dev

How to call stored procedure in ASP.NET MVC with Entity Framework

From Dev

how to get partial view input field value by id using jQuery in asp.net mvc

From Dev

How can I insert data one table and update some data in another table in asp.net mvc 5 & Entity Framework

From Dev

Textbox retains old value before the update process asp.net

From Dev

How to delete the old image in ASP.Net MVC

Related Related

  1. 1

    How do I get an enum value from my ASP.NET MVC property?

  2. 2

    How to get a session value in cshtml file in ASP.Net MVC4

  3. 3

    How to get old entity value in @HandleBeforeSave event to determine if a property is changed or not?

  4. 4

    How to get the value from one table and put into another table using asp.net mvc 4 and jquery

  5. 5

    How to get an enum value ASP.NET MVC

  6. 6

    In ASP.NET MVC 4 Razor, how do you get the value (text) of @Html.Label?

  7. 7

    Using ASP.NET MVC 5, how do I get the localized DisplayAttribute string for an Enum value in a View?

  8. 8

    How to update a collection inside an entity within a post action in ASP.NET MVC5?

  9. 9

    How to Update Model in ASP NET MVC 6?

  10. 10

    Get session value OnConfiguring in asp.net mvc 6 entity framework 7

  11. 11

    Update a record in ASP .Net MVC

  12. 12

    How to Get Custom Property Value of the ApplicationUser in the ASP.Net MVC 5 View?

  13. 13

    How to get a rating value in asp.net mvc4?

  14. 14

    CreateCriteria and old NHibernate in ASP.NET MVC

  15. 15

    How to get Checkbox value in MVC asp.net using JQuery

  16. 16

    How to get the value from one table and put into another table using asp.net mvc 4 and jquery

  17. 17

    How to create users from old asp.net membership to new MVC .net and send password reset

  18. 18

    How to get a value from View to JavaScript in asp.net MVC

  19. 19

    ASP .Net MVC get value from layout

  20. 20

    Using ASP.NET MVC 5, how do I get the localized DisplayAttribute string for an Enum value in a View?

  21. 21

    Update a record in ASP .Net MVC

  22. 22

    update single column of Stub Entity in asp.net mvc

  23. 23

    How can I get value of EditorFor in ASP.Net Mvc and passing value in a button as parameter

  24. 24

    How do I get the Id of a user given the email address in ASP.NET MVC Entity Framework

  25. 25

    How to call stored procedure in ASP.NET MVC with Entity Framework

  26. 26

    how to get partial view input field value by id using jQuery in asp.net mvc

  27. 27

    How can I insert data one table and update some data in another table in asp.net mvc 5 & Entity Framework

  28. 28

    Textbox retains old value before the update process asp.net

  29. 29

    How to delete the old image in ASP.Net MVC

HotTag

Archive