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" });

}

}

Cybercop
  • 8,475
  • 21
  • 75
  • 135
  • yea but does it have to do anything with checking the the values in form being edited or not? – Cybercop Jul 19 '13 at 14:30
  • Are you looking for a javascript solution or a server-side solution? – Jason P Jul 19 '13 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. – Yogiraj Jul 19 '13 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. – Cybercop Jul 19 '13 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 http://jmdority.wordpress.com/2011/07/20/using-entity-framework-4-1-dbcontext-change-tracking-for-audit-logging/ – Yogiraj Jul 19 '13 at 14:44

2 Answers2

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.

Kevin Junghans
  • 17,475
  • 4
  • 45
  • 62
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#?

Community
  • 1
  • 1
JOBG
  • 4,544
  • 4
  • 26
  • 47