1

I have a jQuery function as this:

            $.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');
            }
        });

and my SaveModal action is :

        //#########################  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)  #########################

but when I run my code after filling form , I expect to see my object but I saw undefined. I completely confused and I don't know what is my mistake?

What must I do know?

3
  • Debug the javascript check if the success event handler runs, and inspect the variable e. Debug the c#, check if the action actually runs and returns something. Commented Jul 30, 2014 at 6:47
  • On which line of the script does the error occur? Does it hit your controller? Commented Jul 30, 2014 at 6:47
  • no error occurs and it goes through success but e is undefined!! Commented Jul 30, 2014 at 7:21

2 Answers 2

1

I'm sorry but you are messing up code. You should use model instead of passing each fields.

See below code.

       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);
        }

There is no meaning using ModelState.IsValid since not using Model. Also, you should return json correctly doing like this.

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

Try this to make sure if you avoid undifined object error.

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

I would recommend you to use MODEL. This is what MVC is all about.

    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);
    }

UPDATED

You have use JSON.stringify(object); to map the model. Example

<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>
Sign up to request clarification or add additional context in comments.

1 Comment

dear sir, all of your discussions are true , but these are not my problem, if I put a break point in SaveModal on ** return (varJsonResult);** it is an ok json with data in it, but I can not catch it in my jQuery success function
0

I think it is not able to find the URL. Change URL as below and try

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.