jQuery ajax()在MVC中返回一个未定义的对象

时间:2014-07-30 06:33:04

标签: jquery ajax asp.net-mvc

我有一个jQuery函数:

            $.ajax({
            type: "POST",
            url: "/Accounting/Journal/SaveModal",
            data: varData,
            dataType: "json",
            contentType: "application/json; charset=utf-8",

            success: function (e) {
                alert(e);
            },

            error: function (e, ex) {
                $("div#divModalBody").modal('toggle');
                alert(ex);
            },

            complete: function (e) {
                $("div#myLoadingDialog").modal('toggle');
                $("div#myModal").modal('toggle');
            }
        });

我的 SaveModal 操作是:

        //#########################  SaveModal (POST)  #########################
    [System.Web.Mvc.HttpPost]
    [Infrastructure.ProjectActionPermission
        (isVisibleJustForProgrammer: false,
        accessType: Models.Enums.AccessTypes.Special,
        keyName: Resources.Strings.ActionsKeys.Save)]
    public virtual System.Web.Mvc.JsonResult SaveModal
        (System.Guid journalheaderid, System.Guid account, System.Guid center1,
        System.Guid center2, System.Guid center3, System.Guid project, string description,
        decimal debit, decimal credit, string number, int quantity, int shamsid, int shamsim, int shamsiy)
    {
        Models.Accounting.JournalDetail oJournalDetail =
            new Models.Accounting.JournalDetail();

        if (ModelState.IsValid)
        {
            // **************************************************
            if ((shamsid != 0) &&
                (shamsim != 0) &&
                (shamsiy != 0))
            {
                oJournalDetail.Date =
                    Dtx.Calendar.Convert.PersionToCivil
                    (shamsiy, shamsim, shamsid);
            }
            // **************************************************

            oJournalDetail.JournalHeaderId = journalheaderid;
            oJournalDetail.AccountId = account;

            if (center1 != System.Guid.Empty)
            {
                oJournalDetail.Center1Id = center1;
            }
            if (center2 != System.Guid.Empty)
            {
                oJournalDetail.Center2Id = center2;
            }
            if (center3 != System.Guid.Empty)
            {
                oJournalDetail.Center3Id = center3;
            }
            if (project != System.Guid.Empty)
            {
                oJournalDetail.ProjectId = project;
            }

            oJournalDetail.RowDescription = description;
            oJournalDetail.Debit = debit;
            oJournalDetail.Credit = credit;
            oJournalDetail.Number = number;
            oJournalDetail.Quantity = quantity;
            oJournalDetail.IsPending = true;

            // **************************************************
            oJournalDetail.SetInsertDateTime
                (Infrastructure.Sessions.AuthenticatedUser.Id);

            oJournalDetail.SetIsActive
                (oJournalDetail.IsActive,
                Infrastructure.Sessions.AuthenticatedUser.Id, Infrastructure.Utility.Now);
            // **************************************************

            UnitOfWork.AccountingUnitOfWork.JournalDetailRepository.Insert(oJournalDetail);

            UnitOfWork.Save();
        }

        // **************************************************
        ViewBag.ShamsiD =
            new System.Web.Mvc.SelectList
                (Dtx.Calendar.Day.Days, "Value", "Text", oJournalDetail.ShamsiD);

        ViewBag.ShamsiM =
            new System.Web.Mvc.SelectList
                (Dtx.Calendar.Month.Months, "Value", "Text", oJournalDetail.ShamsiM);

        ViewBag.ShamsiY =
            new System.Web.Mvc.SelectList
                (Dtx.Calendar.ShamsiAccountingYear.Years, "Value", "Text", oJournalDetail.ShamsiY);
        // **************************************************

        var varJsonResult =
            Json(new { oJournalDetail },
            System.Web.Mvc.JsonRequestBehavior.AllowGet);

        ViewBag.JournalDetail = oJournalDetail;

        return (varJsonResult);
    }
    //#########################  SaveModal (POST)  #########################

但是当我在填写表单后运行我的代码时,我希望看到我的对象,但我看到未定义。 我完全糊涂了,我不知道我的错误是什么?

我必须知道什么?

2 个答案:

答案 0 :(得分:1)

我很抱歉,但是你搞砸了代码。你应该使用模型而不是传递每个字段。

见下面的代码。

       public virtual System.Web.Mvc.JsonResult SaveModal
        (System.Guid journalheaderid, System.Guid account, System.Guid center1,
        System.Guid center2, System.Guid center3, System.Guid project, string description,
        decimal debit, decimal credit, string number, int quantity, int shamsid, int shamsim, int shamsiy)
        {
            if (ModelState.IsValid)
            {

            }

            var varJsonResult = Json(new { oJournalDetail }, System.Web.Mvc.JsonRequestBehavior.AllowGet);

            ViewBag.JournalDetail = oJournalDetail;

            return (varJsonResult);
        }

使用ModelState.IsValid没有意义,因为不使用Model。另外,你应该像这样正确地返回json。

return this.Json(oJournalDetail, JsonRequestBehavior.AllowGet);

尝试此操作以确保您是否避免了未定义的对象错误。

return this.Json(new { IsSuccess = true }, JsonRequestBehavior.AllowGet);

我建议你使用MODEL。这就是MVC的全部内容。

    public class YourModel {
        public Guid JournalHeaderId { get; set; }
        public decimal Debit { get; set; }
    }

    [HttpPost]
    public JsonResult SaveModal(YourModel model)
    {
        if (ModelState.IsValid)
        {
            //do something

            return this.Json(new { IsSuccess = true }, JsonRequestBehavior.AllowGet);
        }

        return this.Json(new { IsSuccess = false }, JsonRequestBehavior.AllowGet);
    }

<强>已更新

您已使用JSON.stringify(object);来映射模型。 实施例

<script type="text/javascript">
    var object = new Object();
    object.JournalHeaderId = 'yourguidvaluehere';
    object.Debit = '43.00';

    var objectSerialized = JSON.stringify(object);

    $.ajax({
        type: "POST",
        url: "/Accounting/Journal/SaveModal",
        data: objectSerialized
        //
    });
</script>

答案 1 :(得分:0)

我认为无法找到该网址。如下更改网址,然后尝试

url: window.location.pathname + '/Accounting/Journal/SaveModal'