0

I have method to add new item to db from ajax call

Here is back-end method

public ActionResult AddingInternalAppointment(string Start, string End, string Title, DateTime appointmentDate,int id)
{
    using (var ctx = new ApplicationDbContext())
    {                    
        Appointment appointmentInt = new Appointment()
        {
            Start_appointment = Start,
            End_appointment = End,
            Title = Title,
            Type_of_appointment = "Internal",
            Date = appointmentDate
        };

        ctx.Appointments.Add(appointmentInt);
        ctx.SaveChanges();
        return Json(new {Result = "Success", Message = "Saved Successfully"});
    }

I need to get id of created item and write it to variable.

How I can do this?

mason
  • 31,774
  • 10
  • 77
  • 121
  • Did you check to see if your variable `appointmentInt` had an Id populated after the save? The results may shock you. Also, work on your casing, its all over the place. Use camelCase for parameter names. – maccettura Oct 06 '17 at 20:41
  • Cant you just return back the Id? Like... `return Json(new {Result = "Success", Id = appointmentInt.Id ,Message = "Saved Successfully"});` – penleychan Oct 06 '17 at 20:42

0 Answers0