0

我正在使用 ajax 在 mvc 按钮单击事件上调用控制器操作方法。控制器操作方法具有验证逻辑,如果失败,则应通知用户错误。我不确定如何在 MVC 中实现这一点。有没有办法可以从控制器向 ajax 成功事件发送错误消息?

 Controller Action Method
    [AcceptVerbs(HttpVerbs.Post)]
    public ViewResult Index(ProfileSnapshotRequestModel pspm)
    {

        // Do something 
       return Json(new { success = true });
    }

MVC 视图按钮单击

   $("#copyData").click(function() {

            var selectedRowId = $('#ServersWS').jqGrid('getGridParam', 'selrow');
            var rowData = jQuery("#ServersWS").jqGrid('getRowData', selectedRowId);
            $("#MainDiv").prop('disabled', true);
            var url = '@Url.Action("Index")';
            HideElements();
            $.ajax({
                url: url,
                data: {
                            BaseEnvtId: $("#SelectedEnvironmentID").val(),
                            BaseVersionId: $("#SelectedVersionID").val(),
                            BaseProfileId: rowData['ProfileId'],
                            NewEnvtId: $("#SelectedEnvironmentID2").val(),
                            NewVersionId: $("#SelectedVersionID2").val(),
                            NewProfileId: $("#SelectedProfileID2").val(),
                },
                type: 'POST',
                datatype: 'json',
                success: function(data) {
                    document.getElementById('displaySuccess').style.display = 'block';
                    $("#ProfileCopyDiv").prop('disabled', true);
                    $("#MainDiv").prop('disabled', false);
                },
                error: function() { alert('something bad happened'); }
            });
        });
4

1 回答 1

0

我能够通过将控制器操作返回类型更改为操作结果来解决它,然后使用 Json 对象返回我想要的数据 - 例如

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(ProfileSnapshotRequestModel pspm)
    {
         // Do something 
        return Json(new { success = true });
    }
于 2013-10-14T02:03:25.037 回答