繁体   English   中英

视图未将模型传递给控制器​​-ASP.Net MVC

[英]View is not passing the model to controller - ASP.Net MVC

这是从视图到控制器的值的基本传递,但这并不起作用。当我单击用于更新数据库中记录的功能的更新按钮时,视图中的值未正确将值传递到控制器。 在将调试器放入javascript中后,每个变量都能够正确获取其值,并能够存储它们的对象。

有什么可能导致此问题的原因?

这是Javascript中的按钮onclick事件代码。

 $('#updatePrescription').click(function () {
        debugger;
        ValidateFields();
        var drugListIsEmpty = CheckDrugList();
        var error = $(".text-danger").length;


        if (error == 0 && !drugListIsEmpty) {
            debugger;
            var prescription = [];
            var template = {};

            template.templateName = $("#prescriptionTemplateName").val();
            template.templateTypeId = $('input[name=templateTypeId]:checked').val();
            template.prescriptionTemplateItemList = [];
            template.instructionId = $('.instruction').val();
            template.frequencyId = $('.frequency').val();
            template.day = $('.inputDays').val();
            template.quantity = $('.inputQuantity').val();
            template.dispenseLocationId = $('.selectDispenseLocation').val();
            template.statusId = $('.status').val();
            //template.categoryId = $('.templateCategory').filter(":visible").last().val();
            template.templateId = $('#prescriptionTemplateId').val();

            //if (template.categoryId == null) {
            //    template.categoryId = 0;
            //}

            var x = 0;
            $('#tblPrescriptionSaveTemplateBody tr').each(function (key, value) {
                debugger;
                var row = $(this).closest('tr');
                var next_row = $(row).next();
                var drugId = $(value).find('.drugId').val();
                var dosage = $(value).find('.inputDosage').val();
                var dosageUnitId = $(value).find('.selectUnitId').val();
                var statusId = "41";
                var remarks = $(value).find('.inputDescription').val();
                var groupId = $(value).find('.inputGroupNo').val();
                var unit = $(value).find('.selectUnitId').val();
                var prescriptionTemplateItemId = $(value).find('.prescriptionTemplateItemId').val();

                x++;

                var obj = {
                    // templateId: prescriptionTemplateId,
                    prescriptionTemplateId: template.templateId,
                    prescriptionTemplateItemId: prescriptionTemplateItemId,
                    drugId: drugId,
                    dosage: dosage,
                    dosageUnitId: dosageUnitId,
                    instructionId: template.instructionId,
                    frequencyId: template.frequencyId,
                    day: template.day,
                    quanitity: template.quantity,
                    unit: unit,
                    remarks: remarks,
                    dispenseLocationId: template.dispenseLocationId,
                    groupId: groupId,
                    statusId: template.statusId
                }
                template.prescriptionTemplateItemList.push(obj);
                //prescription.push(obj)

            })

            $.ajax({
                type: 'POST',
                url: '/WestMedicinePrescriptionTemplate/UpdateTemplate',
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(template),
                success: function (data) {
                    ShowNotificationMessage(data.notification);
                    window.location.href = '/WestMedicinePrescriptionTemplate/Index';
                }
            });
        }
    });

预期这将在控制器的参数“ newtemplate”中传递模型的结果,但结果为null

  public ActionResult UpdateTemplate([FromBody] PrescriptionTemplateVM newtemplate)
        {
            int empId = Convert.ToInt32(HttpContext.Session.GetInt32("EmployeeId"));


            var notif = "Update Failed.";
            try
            {
                if (ModelState.IsValid)
                {
                    bool updateSuccessful = _prescription.UpdatePrescriptionTemplateAndItems(newtemplate, empId);

                    if (updateSuccessful)
                    {
                        notif = "Update Successful.";
                    }
                }
            }
            catch (Exception ex)
            {
                notif = ex.Message;
            }
            return Json(new { notification = notif });
        }

代码中可能是什么问题

这样做:

[HttpPost]
public ActionResult UpdateTemplate(PrescriptionTemplateVM newtemplate)

您需要确保使用与在PrescriptionTemplateVM中定义的相同的变量名称。

并且不要将数据转换为Json。 这样做:

$.ajax({
            type: 'POST',
            url: '/WestMedicinePrescriptionTemplate/UpdateTemplate',
            dataType: 'json',
            contentType: 'application/json',
            data: {newtemplate: template},
            success: function (data) {
                ShowNotificationMessage(data.notification);
                window.location.href = 
             '/WestMedicinePrescriptionTemplate/Index';
            }
        });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM