简体   繁体   English

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

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

This is a basic passing of values from view to controller, but this does not seek to work.When I click the update button that functions to update the record in the database, the values from view, does not correctly pass the values to controller. 这是从视图到控制器的值的基本传递,但这并不起作用。当我单击用于更新数据库中记录的功能的更新按钮时,视图中的值未正确将值传递到控制器。 Upon putting debugger in the javascript, the each variables were able to correctly got its values and evn the object where they are stored. 在将调试器放入javascript中后,每个变量都能够正确获取其值,并能够存储它们的对象。

What could possible the reason for this problem? 有什么可能导致此问题的原因?

here's the button onclick event code in 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';
                }
            });
        }
    });

This is expected to pass the result of model in parameter "newtemplate" in the controller, but it results to null 预期这将在控制器的参数“ 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 });
        }

What could be the problem in the code 代码中可能是什么问题

do like this: 这样做:

[HttpPost]
public ActionResult UpdateTemplate(PrescriptionTemplateVM newtemplate)

You need to make sure that you are using the same variables names that you defined in PrescriptionTemplateVM 您需要确保使用与在PrescriptionTemplateVM中定义的相同的变量名称。

and dont convert the data to Json. 并且不要将数据转换为Json。 do like this: 这样做:

$.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.

相关问题 将 javascript 数组从视图传递到 ASP.Net MVC 5 中的控制器 - Passing javascript array from view to controller in ASP.Net MVC 5 ASP.NET MVC 将 model 从视图传递到 controller - ASP.NET MVC Pass model from view into controller 将复杂的 model 传递给 controller 使用 Z9E0DA8438E1E38A1C30F4B76CE3 Core M34A6659BCEAE779F28185E757ABFCA5Z - Passing a complex model to controller using AJAX in ASP.NET Core MVC ASP.NET MVC:将2D数组从Controller传递到View - ASP.NET MVC: Passing 2D array from Controller to View 在 ASP.NET MVC 中将 HTML 表单数据从视图传递到控制器 - Passing HTML form data from View to Controller in ASP.NET MVC ReferenceError: 'x' 未定义 - asp.net mvc 部分视图和 jQuery/JavaScript - 将整个 model 传递给操作方法 - ReferenceError: 'x' is not defined - asp.net mvc partial view and jQuery/JavaScript - passing the entire model to an action method asp.net mvc如何将视图模型和输入文本传递给控制器​​? - asp.net mvc how can I pass the view model and input text to controller? 将AngularJS模型发布到ASP.NET MVC控制器中的复杂模型 - Post AngularJS model to complex model in ASP.NET MVC controller 将动态生成的文本框值传递给ASP.NET MVC中的控制器 - Passing dynamically generated textbox values to controller in ASP.NET MVC 将数据从Ajax传递到ASP.NET MVC中的控制器 - Passing data from Ajax to controller in asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM