0

I have an edit form which has a label and current values in textbox, I want to check if the values in the form has been changed when the form is submitted. Here is the form

<fieldset> <legend>Module <small>Edit</small></legend> @using (Html.BeginForm("Edit", "Module")) { @Html.ValidationSummary(true) @Html.HiddenFor(m=>m.Id) for(var i = 0; i < Model.Properties.Count(); i++) { <label class="label">@Model.Properties[i].Name</label> <div class="input-block-level">@Html.TextBoxFor(model => Model.Properties[i].Value, new { @value = Model.Properties[i].Value })</div> } <div class="form-actions" id="buttons"> <button type="submit" class="btn btn-primary" id="Submit">Save changes</button> @Html.ActionLink("Cancel", "ModuleList", null, new { @class = "btn " }) </div> } </fieldset> 

this results to

enter image description here

How can i check if the form has been changed? My httppost method of controller currently look like this

[HttpPost] public ActionResult Edit(EditModule module) { if (ModelState.IsValid) { _repository.SaveModuleEdits(module); Information("Module was successfully edited!"); return RedirectToAction("ModuleList", "Module", new {area = "Hardware"}); } Error("Edit was unsuccessful, if the problem persists please contact admin!"); return RedirectToAction("ModuleList", "Module", new { area = "Hardware" }); } 

}

5
  • yea but does it have to do anything with checking the the values in form being edited or not? Commented Jul 19, 2013 at 14:30
  • Are you looking for a javascript solution or a server-side solution? Commented Jul 19, 2013 at 14:32
  • I should have also asked for the need to check if it was edited. Just trying to understand so that I can provide proper suggestion. Commented Jul 19, 2013 at 14:33
  • any would work, I just need to be able to forward my form to my httppost action of controller and the action should be able to know if there has been an edit. Commented Jul 19, 2013 at 14:35
  • If I were to guess the need to know if there was edit, I'd say you want to update the records that were changed. Assuming that's what it is. EF has changetracker to keep track of what was changed. I have used it to implement auditing. Something along the lines jmdority.wordpress.com/2011/07/20/… Commented Jul 19, 2013 at 14:44

2 Answers 2

0

It is fairly straight forward on the client side if you using something like Knockout. Here is an article that describes how to use Knockout for change tracking. This article uses a Knockout add-on called KoLite to make it even simpler.

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

Comments

0

One way to check if a value has changed from its original state (server side), is through HMAC mechanism.

Basically it generates a hash based on a string and secret key, and this hash is sent along with the form as a hidden field (http get), if the value is changed by the customer then the recalculation of the hash (http post) will be different from what is stored in the hidden field, then you know that someone change the value of that field.

This may be a little overworked but is one of the safest methods.

https://security.stackexchange.com/questions/20129/how-when-do-i-use-hmac

How to generate HMAC-SHA1 in C#?

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.