2017-10-06 14 views
-2

が含まれている私はここでAajax要求パラメータ辞書は、パラメータにnullエントリ(ASP.NET)

にDBにデータを追加する方法は、コードは、バックエンド

public ActionResult AddingInternalAppointment(string Start, string End, string Title, DateTime Date,int id) 
    { 

     using (var ctx = new ApplicationDbContext()) 
     { 

      Appointment appointmentInt = new Appointment() 
      { 
       Start_appointment = Start, 
       End_appointment = End, 
       Title = Title, 
       Type_of_appointment = "Internal", 
       Date = Date 
      }; 
      ctx.Appointments.Add(appointmentInt); 
      ctx.SaveChanges(); 
      return Json(new {Result = "Success", Message = "Saved Successfully"}); 
     } 
    } 

上にあり、ここにAJAXである必要があり前面に要求 - エンド:私はこの関数を呼び出すと

function addAppointmentInternal() { 
    var idofdoctor = moment($('#startAppointment').val()).toISOString(); 
    alert(idofdoctor); 
    $.ajax({ 
     type: 'Post', 
     dataType: 'Json', 
     data: { 
      Start: $('#startAppointment').val(), 
      End: $('#endAppointment').val(), 
      Title: $('#title').val(), 
      Date: moment($('#startAppointment').val()).toISOString() 
     }, 
     url: '@Url.Action("AddingInternalAppointment","Calendar")', 
     success: function (da) { 
      if (da.Result === "Success") { 
       $('#calendar').fullCalendar('refetchEvents'); 
       $("#myModal2").modal(); 
      } else { 
       alert('Error' + da.Message); 
      } 
     }, 
     error: function(da) { 
      alert('Error'); 
     } 
    }); 
} 

それは私に、このエラーを示しているが、私は日中の値を持っています。

どうすれば修正できますか?

enter image description here

+0

に応じ

[HttpPost] public ActionResult AddingInternalAppointment([FromBody]AppointmentOptions model) { if(ModelState.IsValid) { string Start = model.Start; string End = model.End; //... //...code removed for brevity } 

次のアクションが(何か他にパラメータ名の日付を変更してみてくださいアクション

​​

更新のためのモデルを作成します。 alike appointmentDate)、それはキーワードのように思われるので、パラメータとして使用することはできません。あなたはajax呼び出しで同じものを変更する必要があります。 – DSR

+0

ajaxを呼び出すときに '$( '#startAppointment')。val()'の値は何ですか? – Wndrr

+0

この値に誤りがあります@Wndrr –

答えて

1

何か他のもの(appointmentDateのような)にパラメータ名Dateを変更してみてください。あなたはajax呼び出しで同じものを変更する必要があります。

+1

これはナンセンスです。 'Date'は予約されたキーワードではありません。どのように地球上で投票し、受け入れたか?そして参考までに、[ここに予約されたキーワードがあります](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/) –

1

いくつかあります。

クライアント更新AJAX呼び出し

function addAppointmentInternal() { 
    var idofdoctor = moment($('#startAppointment').val()).toISOString(); 
    var model = { 
      Start: $('#startAppointment').val(), 
      End: $('#endAppointment').val(), 
      Title: $('#title').val(), 
      Date: moment($('#startAppointment').val()).toISOString() 
     }; 
    alert(idofdoctor); 
    $.ajax({ 
     type: 'Post', 
     dataType: 'Json', 
     data: JSON.stringify(model), //<-- NOTE 
     url: '@Url.Action("AddingInternalAppointment","Calendar")', 
     success: function (da) { 
      if (da.Result === "Success") { 
       $('#calendar').fullCalendar('refetchEvents'); 
       $("#myModal2").modal(); 
      } else { 
       alert('Error' + da.Message); 
      } 
     }, 
     error: function(da) { 
      alert('Error'); 
     } 
    }); 
} 
関連する問題