2017-10-06 41 views
-2

我有沒有辦法將數據添加到上Aajax要求DB參數字典包含參數(ASP.NET)無效項

這裏是在後端

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

嘗試改變參數的名稱日期到別的模型(類似的預約日期),它好像是關鍵字,所以你不能用它作爲參數。你需要在ajax調用中改變它。 – DSR

+0

在調用ajax時'$('#startAppointment')。val()'的值是多少? – Wndrr

+0

錯誤不在此值@Wndrr –

回答

1

嘗試改變參數名Date別的東西(如appointmentDate)。你需要在ajax調用中改變它。

+1

這是無稽之談。 「日期」不是保留關鍵字。這究竟是如何得到投票和接受的。和FYI,[這裏是保留的關鍵字](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/) –

1

有幾件事。

創建行動

​​

更新客戶端更新Ajax調用動作相應

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

下一頁

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'); 
     } 
    }); 
} 
相關問題