javascript函数传递具有空值的对象

时间:2019-04-24 06:35:43

标签: javascript c# asp.net-mvc master-detail

我正在使用此article作为指导进行主/详细操作,我的情况有所不同,因为我对单个主表有五个不同的详细信息,而每个主表具有完全不同的详细信息/列。我希望用户输入所选明细表中的所有行,并将这些行与主表一起传递给控制器​​,但传递的参数始终为null。我搜索了很多但没有成功。

post中的解决方案对我和其他论坛中的其他几个用户均无效。

这是我的代码: // JobOrderMaster操作:

[HttpPost]
        public ActionResult JobOrderMaster(WorkOrder objJobOrder, string btnPrevious, string btnNext)
        {
            ViewBag.Project_Id = new SelectList(db.Projects, "Project_Id", "Project_Name_e");

            if (btnNext != null)
            {
                if (ModelState.IsValid)
                {
                    workOrderId = Guid.NewGuid();
                    JobOrderViewModel objWorkOrder = GetJobOrder();
                    objWorkOrder.WorkOrder_Id = workOrderId;
                    objWorkOrder.Start_Date = objJobOrder.Start_Date;
                    objWorkOrder.End_Date = objJobOrder.End_Date;
                    objWorkOrder.Project_Id = objJobOrder.Project_Id;
                    objWorkOrder.Subject = objJobOrder.Subject;
                    objWorkOrder.WorkOrder_Date = DateTime.Now;
                    objWorkOrder.Created_Date = DateTime.UtcNow;
                    objWorkOrder.WorkOrder_Type = objJobOrder.WorkOrder_Type;
                    objWorkOrder.Created_By = User.Identity.GetUserName();
                    objWorkOrder.Descriptions = objJobOrder.Descriptions;

                    ViewBag.WorkOrderId = objWorkOrder;


                    switch ((int)objJobOrder.WorkOrder_Type)
                    {
                        case 1: return View("JobOrderDetails");
                        case 2: return View("InHouseMaintenance");
                        case 3: return View("CustomerSideMaintenance");
                        case 4: return View("SiteSurvey");
                        case 5: return View("Installation");
                        default: return View("JobOrderDetails");
                    }
                }
            }

            return View();
        }

//JobOrderDetails Action:

        //string expenseMaster;
        [HttpPost]
        public ActionResult JobOrderDetails(WorkOrderDetail[] objWorkOrderDetail)
        {
            WorkOrder objWorkOrder = new WorkOrder();
            WorkOrderDetail details = new WorkOrderDetail();
            JobOrderViewModel objJobOrder = GetJobOrder();

            if (objJobOrder.WorkOrder_Id != null)
            {
                objWorkOrder.WorkOrder_Id = objJobOrder.WorkOrder_Id;
                objWorkOrder.Start_Date = objJobOrder.Start_Date;
                objWorkOrder.End_Date = objJobOrder.End_Date;
                objWorkOrder.Project_Id = objJobOrder.Project_Id;
                objWorkOrder.Subject = objJobOrder.Subject;
                objWorkOrder.Descriptions = objJobOrder.Descriptions;
                objWorkOrder.Type_Id = objJobOrder.Type_Id;
                objWorkOrder.WorkOrder_Type = objJobOrder.WorkOrder_Type;
                objWorkOrder.WorkOrder_Status = objJobOrder.WorkOrder_Status;
                objWorkOrder.WorkOrder_Priority = objJobOrder.WorkOrder_Priority;
                objWorkOrder.WorkOrder_Date = DateTime.Now;
                objWorkOrder.Created_Date = DateTime.UtcNow;
                objWorkOrder.Created_By = User.Identity.GetUserName();

                db.WorkOrders.Add(objWorkOrder);
            }

            int detailId = 1;
            //if (btnNext != null)
            //{
            if (ModelState.IsValid)
            {
                foreach (var item in objWorkOrderDetail)
                {
                    details.WorkOrder_Id = objWorkOrder.WorkOrder_Id;
                    details.Category_Id = item.Category_Id;
                    details.Detail_Id = detailId;
                    details.Qty = item.Qty;

                    db.WorkOrderDetails.Add(details);

                    detailId++;
                }

                try
                {
                    db.SaveChanges();
                    RemoveJobOrder();
                    return View("Index");
                }
                catch (DbUpdateException ex)
                {
                    string message = "";
                    UpdateException updateException = (UpdateException)ex.InnerException;
                    SqlException sqlException = (SqlException)updateException.InnerException;

                    foreach (SqlError error in sqlException.Errors)
                    {
                        message = string.Format("{0} Error '{1}' occurred in {2} at {3}\n\n\r\n",
                                     message
, error.Number
, error.Message
, error.LineNumber);
                    }

                    ViewBag.errorMessage = message;
                    clsGeneralMethods.ErrorLog(message);
                    return View("Error");
                }
                catch (DbEntityValidationException ex)
                {
                    string message = "";
                    foreach (DbEntityValidationResult item in ex.EntityValidationErrors)
                    {
                        //Get entry
                        DbEntityEntry entry = item.Entry;
                        string entityTypeName = entry.Entity.GetType().Name;

                        //Display or log error messages
                        foreach (DbValidationError subItem in item.ValidationErrors)
                        {
                            message = string.Format("{0}Error '{1}' occurred in {2} at {3}\n\n\r\n",
                                     message
, subItem.ErrorMessage
, entityTypeName
, subItem.PropertyName);
                        }
                    }

                    ViewBag.errorMessage = message;
                    clsGeneralMethods.ErrorLog(message);
                    return View("Error");
                }
            }
            //}
            return View();
        }

//My javascript functions
function btnFinish(details) {
                //alert(JSON.stringify(details));
                return $.ajax({
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    type: 'POST',
                    url: '@Url.Action("JobOrderDetails")',
                    data: JSON.stringify({ objWorkOrderDetail: details }),

                    success: function (result) {
                        //location.reload();
                    },
                    error: function (err) {
                        if (err != null) {
                            alert('Error: ' + err.Message);
                        }
                    }
                });
            }

            $("#btnFinish").click(function (e) {
                e.preventDefault();
                var claimArr = [];
                claimArr.length = 0;

                $.each($("#detailsTable tbody tr"), function () {
                    claimArr.push({
                        categoryId: $(this).find('td:eq(0)').html(),
                        descriptions: $(this).find('td:eq(1)').html(),
                        quantity: $(this).find('td:eq(2)').html()
                    });
                });

                var data = JSON.stringify(claimArr);
                //alert(data);

                $.when(btnFinish(data)).then(function (response) {
                    window.location.href = "../JobOrder/Index";
                    //console.log(response);
                }).fail(function (err) {
                    console.log(err.Message);
                    window.location.href = "../JobOrder/Index";
                });
            });
        });

action screenshot shows null values alert msg shows object received ok in js function

0 个答案:

没有答案