0

I currently have the below javascript to detect changes in my form.

form.addEventListener("input", function () {
        ChangesMade = true;
        console.log("Change");
    });

I now need to include whether or not a change has been made in a call to the controller which means that the value can no long be stored in javascript as this is client side.

3
  • Do you need to know if the actual model contents has changed when posted or just if someone typed in a textbox regardless of whether a new value was entered. Commented Dec 6, 2017 at 13:56
  • just if someone has typed in a text field for example if someone changes a sentence from "There a 2 types of ..." to "There are 3 types of ..." Commented Dec 6, 2017 at 13:58
  • OK.. That would be the model content changing then. Either answer below seems fine then. Commented Dec 6, 2017 at 14:02

2 Answers 2

1

You have a couple options...

1. Construct a view model with two instances of your model. One the "original" instance, one the "changed" instance. Bind the "original" fields to hidden inputs in your form, and bind the "changed" fields to your normal form elements.

Then, when the form is posted to the server, your controller action's input would have two instances to compare. If the "changed" values are different from the "original" values, then the user changed something.

Note however that users can still change hidden inputs if they want to. This is not a secure measure, but can be an effective one if "security" isn't a concern here.

2. Keep your view and controller interaction the same as it is, but when the data is posted to the controller you would use the identifier from that record to fetch the original from the database.

Using the posted model and the database-fetched model, compare the two. If values are different then the user changed something.

Note however that this doesn't cover race conditions. It's possible that another user changed the database values since this model was originally shown to this user. You can get around this by adding timestamps of when data was changed, but it might not be enough to tell you what data was changed unless you keep audit copies of old records. The complexity of that escalates quickly.


It's a trade-off between the two options of which one meets the actual needs of the system with acceptable drawbacks.

Sign up to request clarification or add additional context in comments.

Comments

1
  1. Send the form data to the server
  2. On the server side load current data from the DB
  3. Compare and test for changes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.