Search code examples
javascripthtmlasp.net-mvc-4inline-editing

Editing A Table In MVC using Javascript


I am very new to MVC and coding. I have a table that I created in using MVC, but I am wanting to hook up JavaScript to enable inline editing and post back to the database. I think I have been watching too many tutorials, that I am now confused on how to even execute this. Is there anyone out there who could give me a launch in the right direction as to how to begin connecting JavaScript so that I can accomplish inline editing.


Solution

  • Here is how I resolved my question. This is for anyone who is wanting to know how to do inline editing of a table that you created using html.

    Here is my code for the function I created once you clicked on Edit:

    function editThis(element) {
    var customerID = $(element).closest('tr').find('.customer-stored-   id').attr('data-value');
    GetByID("Home/Edit", customerID, replaceCustomerRow, element);
    

    }

    This function replaces the row

    function replaceCustomerRow(result, element) {
    $(element).closest('tr').replaceWith(result);
    

    }

    This function will get the ID

    function GetByID(url, id, callback, param1) {
    
    $.ajax({
        url: "../../" + url + "/" + id,
        type: "GET",
        success: function (result) {
            if (callback != null && callback != undefined) {
                callback(result, param1);
            }
        },
        error: function (result) {
            if (result.responseText != '') {
                alert(result.responseText);
            }
            else {
                alert("An error occurred while processing results.  Please consult an administrator.");
            }
        }
    })
    

    }

    Then you would just write your code for the controller. Simple enough, but it works.