2

I am using ASP.NET MVC 3 and have a need to detect if the form field has been changed on the server side. I know about using tricks with hidden fields, but I was wondering if there is a way to do it by just using the API?

Basically, I have edit screen for my model and one of the fields is an optional id that can be specified. If the field is specified, I have to insure it is unique (no other model has it). So on the edit controller, I want to run the validation but only if that field has been changed.

Please note, I don't need to know previous value vs. new value, just if the field value has changed.

6
  • 1
    I'm not greatly familiar with the MVC pattern but I would set up a javascript to catch changes to the textbox and use AJAX to check if that id already exists or not. Commented Apr 26, 2011 at 16:08
  • Why not just run the validation every time? Commented Apr 26, 2011 at 16:35
  • @hyp js is ok, but I need also server side solution (as stated). MVC3 has a attribute @Remote that can be used for AJAX remote validation Commented Apr 26, 2011 at 16:44
  • @jfar If the field contains optional id (e.g. 123) and has not been changed, then I don't have to make a DB call. If it has been changed, then I make a DB call to see if another customer has the same id (thus, raising model validation error) Commented Apr 26, 2011 at 16:46
  • @zam6ak - An extra select by ID call during writes won't hurt you. I'd say its an easy trade off for the complexity you are introducing with dirty tracking. Commented Apr 26, 2011 at 16:49

2 Answers 2

5

There is indeed no 'dirty' flag - MVC actually is closer to "the way the web works" to reuse that statement. All that is sent over are name value pairs. nothing else. MVC's model binder just matches those names to your object - so in order to truly detect a change you have to either validate against the true data source upon post or compare values passed in on the form - in which case - it is best to hash to avoid forgery.

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

Comments

4

You will have to keep a copy of the old value somewhere, and do the comparison. You may store it in your View Model.

4 Comments

Hmm...Is there no other way?I was hoping MVC3 API may have something that would set a "dirty" flag on the field...
No, you can use any class as your Model, it doesn't need to inherit from anything or other... So, if you want to track changes, you'll have to store the old value in the Model, and when the user posts the form, compare the values.
what you suggest is the "hidden field"/"hidden field w/ hash" approach that I was hoping to avoid...But I may not have an option it seems...
Indeed. You may alternatively store the previous version of the entire form in a Session variable, but that is definitely not a best practice. It's way better to store the old values in the view model, marked as hidden.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.