1

I am using MVC4 ,jquery,ajax and json,I have task

In registration form ,fetch the data already entered the data for editing.I edited the content.Then I click on cancel button ,the form data is reset ed to before edited data.

Is any way to avoid the db call to reset the values.? I heard jquery maintaining a history data options for resetting values.

2 Answers 2

1

The first time you make a DB call for obtaining the values..... store these values as JSON objects in the client side. When the user edits the data these objects will be able to maintain their state and hence will remain unchanged. Hence on clicking cancel fetch the original values from the JSON objects stored in the client side. Thus in this case the entire flow would be in the client side and no server call would be made.

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

1 Comment

Good idea. A nice way to implement this may to be to create custom MVC HtmlHelper extensions like ResettableTextboxFor() that would basically create the textbox, but automatically add a data-init-val attribute to the element that stores its initial value, which you could then just iterate through all of the input elements and reset value on Cancel button press.
0

I believe you could have a generic method to save the current value (and other attributes, like checked) of several inputs, and another to reverse the value to that saved value, using jQuery data:

function save() {
    $("input, textarea, select").each(function() {
        $(this).data("savedVal", $(this).val());
        $(this).data("savedChecked", $(this).prop("checked"));
    });
}

function reset() {
    $("input, textarea, select").each(function() {
        $(this).val($(this).data("savedVal"));
        $(this).prop("checked", $(this).data("savedChecked"));
    });
}

Demo. I'd suggest constraining your selector to a specific form though, instead of capturing all inputs in the whole page.

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.