0

我有以下代码可以打开与视图的对话。

$('.bulkEditlink').click(function () {
    var f = $('td:first', $(this).parents('tr')).text();
    var r = tableCols().toString();
    loadBulkEdit(f, r); //loadBulkEdit is defined on RoleCompare View.
});

--用get加载视图

function loadBulkEdit(f,r) {
        var  $urlpath = "@Url.RouteUrl(new { area = "Admin", controller = "Role", action = "RoleEntitlementEdit"})";
        $.ajax({
            url: $urlpath,
            type: 'GET',
            data:  { 
                        funct: f, 
                        roleName: r, 
                        access: 'access' 
                    },
            OnFailure: "alert('error')",
            success: function (data) {
                $("#ajax-content").html(data);
                loadAccess();
            }
        });
} //end loadBulkEdit

——对话框。保存时,调用 SaveRoleEntitlement 操作方法(视图上定义的 Ajax.BeginForm 选项

function loadAccess(xhr, status) {
        $('#ajax-content').dialog({
            modal: true,
            width: 370,
            title: $('#ajax-content legend').text(),
            buttons: {
                "Save": function () {
                    $('#ajax-content form').submit();
                   $(this).dialog('destroy').html('');
                },
                "Cancel": function () {
                    $(this).dialog('destroy').html('');
                }
            }
        });
    } //end popup

--控制器动作

public JsonResult SaveRoleEntitlement(RoleEntitlementEidtModel model)
        {
            try
            {

                string strPackageName = model.RoleName;
                string strFebSecID = User.Identity.Name;
                string strKeyValue = "";
                string strFunction = model.Function;
                string strAccessLevel = model.AccessLevel;

                PatService.EditEntitlement(strFebSecID, strPackageName, strFunction, strAccessLevel, strKeyValue);
                return Json(new { Error = string.Empty });
            }
            catch (Exception ex)
            {
                return Json(new { Error = ex.Message });
            }
        }

这很好用,只是我正在努力添加 1. 保存时的错误处理。如果有任何异常,我想向用户显示错误消息 2. 方法执行时进度条或某种“等待”消息。希望有人能帮助我。

谢谢。

4

1 回答 1

1

这实际上取决于您要显示错误消息的方式/位置。第一种可能性是在发生错误时返回 JSON(就像您当前所做的那样),然后订阅您的 OnSuccess 处理程序Ajax.BeginForm来处理这种情况:

@using (Ajax.BeginForm("SaveRoleEntitlement", "Role", new { area = "Admin" }, new AjaxOptions { OnSuccess = "saveRoleEntitlementSuccess" }))
{
    ...
}

然后定义这个成功回调:

function saveRoleEntitlementSuccess(result) {
    if (result.Error && result.Error != '') {
        // there was an error => show it somehow
        alert(result.Error);
    } else {
        // the save was successful => do whatever you need to do in this case
    }
}

如果您想直接在 UI 上处理验证错误,另一种可能性是在出现错误时返回相同的部分:

public ActionResult SaveRoleEntitlement(RoleEntitlementEidtModel model)
{
    if (!ModelState.IsValid)
    {
        // there was a validation error => redisplay the partial form
        // so that fields appear in red
        return PartialView("RoleEntitlementEdit", model);
    }
    try
    {
        string strPackageName = model.RoleName;
        string strFebSecID = User.Identity.Name;
        string strKeyValue = "";
        string strFunction = model.Function;
        string strAccessLevel = model.AccessLevel;

        PatService.EditEntitlement(strFebSecID, strPackageName, strFunction, strAccessLevel, strKeyValue);
        return Json(new { success = true });
    }
    catch (Exception ex)
    {
        // we're gonna show this in a ValidationSummary
        ModelState.AddModelError("", ex.Message);
        return PartialView("RoleEntitlementEdit", model);
    }
}

和你的 Ajax 表单:

@using (Ajax.BeginForm("SaveRoleEntitlement", "Role", new { area = "Admin" }, new AjaxOptions { OnSuccess = "saveRoleEntitlementSuccess" }))
{
    @Html.ValidationSummary()
    ...
}

saveRoleEntitlementSuccess这次你的功能:

function saveRoleEntitlementSuccess(result) {
    if (result.success) {
        // the save was successful => do whatever you need to do in this case
    } else {
        // there was an error => in this case the controller action returned
        // a partial that we could use to refresh the form in the DOM:
        $('#ajax-content').html(result);
    }
}
于 2012-08-16T16:41:28.670 回答