Hi,
I am trying to edit an event in the calender. It is not saving and error says that can't save to DB and the field Description is empty.
the error:
SqlException: Cannot insert the value NULL into column 'theDescription', table 'aspnet-ExpenseTracker-63f88f9b-d2f8-444d-a998-9ce2ab1e3f81.dbo.CalEvents'; column does not allow nulls. INSERT fails.
Home Controller Code:
[HttpPost]
public JsonResult SaveEvent(CalEvents e)
{
var status = false;
if (e.EventId > 0)
{
var v = this.Context.CalEvents.Where(a => a.EventId == e.EventId).FirstOrDefault();
if (v != null)
{
v.theSubject = e.theSubject;
v.theDescription = e.theDescription;
v.theStart = e.theStart;
v.theEnd = e.theEnd;
v.theThemeColor = e.theThemeColor;
v.theIsFullDay = e.theIsFullDay;
}
}
else
{
this.Context.CalEvents.Add(e);
}
this.Context.SaveChanges();
status = true;
return new JsonResult(new {status = status});
}
and this is the code in index page:
$('#btnEdit').click(function () {
//Open modal dialog for edit event
openAddEditForm();
function openAddEditForm() {
if (selectedEvent != null) {
$('#hdEventID').val(selectedEvent.eventID);
$('#txtSubject').val(selectedEvent.title);
$('#txtStart').val(selectedEvent.start.format('DD/MM/YYYY HH:mm A'));
$('#chkIsFullDay').prop("checked", selectedEvent.allDay || false);
$('#chkIsFullDay').change();
$('#txtEnd').val(selectedEvent.end != null ? selectedEvent.end.format('DD/MM/YYYY HH:mm A') : '');
$('#txtDescription').val(selectedEvent.description);
$('#ddThemeColor').val(selectedEvent.color);
}
$('#myModal').modal('hide');
$('#myModalSave').modal();
}
$('#btnSave').click(function () {
//Validation/
if ($('#txtSubject').val().trim() == "") {
alert('Subject required');
return;
}
if ($('#txtStart').val().trim() == "") {
alert('Start date required');
return;
}
if ($('#chkIsFullDay').is(':checked') == false && $('#txtEnd').val().trim() == "") {
alert('End date required');
return;
}
else {
var startDate = moment($('#txtStart').val(), "DD/MM/YYYY HH:mm A").toDate();
var endDate = moment($('#txtEnd').val(), "DD/MM/YYYY HH:mm A").toDate();
if (startDate > endDate) {
alert('Invalid end date');
return;
}
}
var data = {
EventID: $('#hdEventID').val(),
Subject: $('#txtSubject').val().trim(),
Start: $('#txtStart').val().trim(),
End: $('#chkIsFullDay').is(':checked') ? null : $('#txtEnd').val().trim(),
Description: $('#txtDescription').val(),
ThemeColor: $('#ddThemeColor').val(),
IsFullDay: $('#chkIsFullDay').is(':checked')
}
SaveEvent(data);
// call function for submit data to the server
})
function SaveEvent(data) {
$.ajax({
type: "POST",
url: '/home/SaveEvent',
data: data,
success: function (data) {
if (data.status) {
//Refresh the calender
FetchEventAndRenderCalendar();
$('#myModalSave').modal('hide');
}
},
error: function () {
alert('Failed');
}
})
}
Thanks