2017-08-03 123 views
0

这是从视图到控制器的值的基本传递,但这并不寻求工作。当我单击更新按钮,该函数更新记录在数据库中,来自视图的值不会将值正确传递给控制器​​。将调试器放入JavaScript中后,每个变量都能够正确地获取其值并确定它们存储的对象。视图不传递模型到控制器 - ASP.Net MVC

这个问题的原因是什么?

这里是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”模型的结果,但它的结果为空

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

可能是什么问题代码

+0

将'[HttpPost]'添加到您的控制器操作以启动。 https://stackoverflow.com/questions/5980389/proper-way-to-use-ajax-post-in-jquery-to-pass-model-from-strongly-typed-mvc3-vie –

+1

不要转换'模板var变成JSON字符串。直接将其设置为数据:'data:template'。 –

回答

1

做像这样:

[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'; 
      } 
     }); 
相关问题