Reputation: 76
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?
Upvotes: 1
Views: 927
Reputation: 7525
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>
Upvotes: 1
Reputation: 9
I think it is not able to find the URL. Change URL as below and try
url: window.location.pathname + '/Accounting/Journal/SaveModal'
Upvotes: 0