2017-08-03 16 views
0

これはビューからコントローラへの基本的な値の受け渡しですが、これはうまくいきません。更新ボタンをクリックすると、ビューの値であるデータベースは、値をコントローラに正しく渡しません。デバッガをjavascriptに置くと、各変数はその値を正しく取得でき、オブジェクトが格納されている場所に格納されます。ビューはモデルをコントローラに渡していません - ASP.Net MVC

この問題の原因として考えられる原因は何ですか?

ここでは、ボタンのonclickイベントコードはJavascriptで表示されます。

$('#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/properway-to-use-ajax-post-in-jquery-to-pass-model-from-strongly-typed-mvc3-vie –

+1

' template'をJSON文字列に変換します。単にデータとして設定してください: 'data:template'。 –

答えて

1

行うnullになりされます

同じ変数名を使用していることを確認する必要があります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'; 
      } 
     }); 
関連する問題