Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Validation Errors with AJAX Calls - ASP.NET

RSS
Modified on Fri, Aug 05, 2011, 2:03 PM by Administrator Categorized as ASP·NET Web Forms

JavaScript

$.ajax({
    type: 'GET',
    url: '/Admin/SaveMyStuff',
    async: false,
    datatype: 'json',
    data: data,
    success: function (response) {
        if (response.length > 0) {
            alert(response);
        }
        else {
            alert('Data saved successfully');
            ajaxReloadElement('/Admin/MyGrid', { id: getId() },
                            '#AdminGridDiv');
        }
    } /* success*/
}); /* ajax */

. . .

/*===========================================================================================*/
function ajaxReloadElement(url, data, targetSelector, httpMethod) {

    if (httpMethod == undefined || httpMethod == null) {
        httpMethod = 'GET';
    }

    $.ajax({
        type: httpMethod,
        url: url,
        async: false,
        datatype: 'json',
        data: data,
        success: function (response) {

            $(targetSelector).html(response);

        } /* success */
    });   /* ajax */
}

Controller Code

public JsonResult SaveMyStuff(MyModel model)
{
    if (!ModelState.IsValid)
    {
        var response = (from ms in ModelState.Values
                        from err in ms.Errors
                        select err.ErrorMessage).ToList().FirstOrDefault();

        return Json(response, JsonRequestBehavior.AllowGet);
    }

    model.Save();
    return Json("", JsonRequestBehavior.AllowGet);
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2018, Patrick Jasinski.