Search code examples
asp.net-mvchtml-helper

How to undo pending changes in MVC Textboxfor?


I want to have a Html.Textboxfor in Edit form. The textbox will be mapped with data coming from model. I also want to have a button to undo changes and revert back to what was mapped before.

How do I implement this?


Solution

  • @frozenmirage A few different ways you can do this. You might try loading the initial value into a hidden input and grab it when your 'undo' button is clicked.. like this.

    Razor

    <input type="hidden" id="TextBoxValue" value="@Model.yourtextboxattributename" />
    

    Script

    $('#undo').click(function(){
        var value = $('#TextBoxValue').val();
        $('#yourtextboxattributename').val("");
        $('#yourtextboxattributename').val(value);
    });