1

I have a form with two textboxes. I am retrieving data from the database to populate the boxes. When my user clicks on submit button and the content of the 2 textboxes does not change, I dont want to go through the code.

How do I determine when the content of the boxes changes and when it does not change? Do I need to make some kind of comparison to what I have in memory?

 public ActionResult Edit(profile objprofiler) { if (ModelState.IsValid) { //Go fetch the existing profile from the database var currentProfile = db.Profiles.FirstOrDefault(p => p.ProfileId == objprofiler.ProfileId); //Update the database record with the values from your model currentProfile.City = objprofiler.City; currentProfile.State = objprofiler.State; //Commit to the database! db.SaveChanges(); ViewBag.success = "Your changes have been saved"; return View(profiler); } } 

3 Answers 3

1

You can compare the values with a simple if condition. Something like this:

if ((currentProfile.City != objprofiler.City) || (currentProfile.State != objprofiler.State)) { currentProfile.City = objprofiler.City; currentProfile.State = objprofiler.State; db.SaveChanges(); } 

Or use whatever logic you're trying to achieve, really. Whether you want to compare for each field individually, use a && instead of an ||, etc. The logic you want to implement is up to you. But you'd perform the comparison in an if statement.

Note also that you can use string.Equals() instead of just the == operator to compare strings with some more options, such as case sensitivity options and other useful things.


If the comparison gets more complex, you might also encapsulate it in the profile object itself. Perhaps by overriding .Equals(), though that has other implications when testing for equality. Maybe just a simple helper function:

public bool IsEqualTo(profile obj) { return this.City == obj.City && this.State == obj.State; } 

Then in the controller you can just use that method:

if (!currentProfile.IsEqualTo(objprofiler)) db.SaveChanges(); 
Sign up to request clarification or add additional context in comments.

10 Comments

so what happens if I have a form with 40 fields. Will I be comparing 40 fields? There has to be a better solution to do this. Dont you think so?
@user2320476: What exactly are you trying to accomplish here? If you want to make 40 comparisons, then you have to make 40 comparisons. You can encapsulate the comparison in the object itself, so that in the controller it's a single line of code to invoke that functionality. (Overriding .Equals(), perhaps?) I guess the question really becomes... Is all of this extra code worth saving one trip to the database? What is really accomplished by saving that trip to the database? If the performance is that bad then the problem may be somewhere else.
What I am saying is that there has to be a way to determine changes on a form with limited code and not comparing 40 fields with an if statement. Thanks.
@user2320476: Define "changes on a form". You have two objects, each with 40 fields. And you're trying to compare the values of those fields. You can wrap it in helper functions and other things, but at some level if you want to compare 40 fields then you're going to have to write 40 comparisons. There's no other way to compare values than by, well, comparing values.
How about INotify? Definition of changes on a form is this. I have 40 textboxes that were populated with data from the database. I have a submit button on a form. If I click submit button and change 20 textboxes values out of the 40 I want to update the database. If I do not change any values on the form and click on submit button , database should not be updated
|
0

The way I typically handle this is by setting a 'dirty' flag any time a data change event occurs on any of the form's controls.

When the user comes to submit the form, I just check the state of the flag to see whether any changes need to be saved. This avoids having to compare all data to their previous states, which can be a nuisance if there are a lot of input controls on the form.

For example:

 bool isDirty; private void textBox_TextChanged(object sender, EventArgs e) { // Possible validation here SetDirty(true); } private void SetDirty(bool dirty) { // Possible global validation here isDirty = dirty; } private void Submit() { if(isDirty) { // Save logic } } 

This approach allows you to run any global validation logic whenever any data is changed.

Caveat: If a user makes a change then reverts it, the form will still submit the data.

3 Comments

textBox_TextChanged ? This doesn't look like MVC to me. Though it certainly does bring up the possibility of performing this logic client-side, which is another option for the OP. If he doesn't care whether or not users bypass the logic, that is.
Yeah textBox_TextChanged is windows development right? This is not mvc right?
Apologies, I misunderstood the question. I guess you can pretty much disregard this.
0

On the client side you can check if the value has changed by running some js to compare the elements value to its initial value. Something like this.

function hasFormChanged() { //textboxes, textareas var els = document.querySelectorAll('input[type="text"], textarea, input[type="number"]'); for (i = 0; i < els.length; i++) { var el = els[i]; if (el.value !== el.defaultValue) { return true; } } //checkboxes and radios els = document.querySelectorAll('input[type="radio"], input[type="checkbox"]'); for (i = 0; i < els.length; i++) { var el = els[i]; if (el.checked !== el.defaultChecked) { return true; } } //select els = document.querySelectorAll('select'); for (i = 0; i < els.length; i++) { var el = els[i]; if (el.options[el.selectedIndex].value != '') { if (!el.options[el.selectedIndex].defaultSelected) { return true; } } } //if we get here then nothing must have changed return false; } 

and it that function return true indicating that something has changed you can set a hidden form value like this

 <input type="hidden" value="false" id="AnyUpdates" name="AnyUpdates"/> 

to true.

Then in your controller update read that field to determine if you need to do your db stuff.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.