I have this code to get vacation data from form for specific employee Id that I took his id from URL; now what is the right way to save that data for this employee? I have a list of employees; when I click on one, the code shows a list of vacations associated with this employee; I want to add one more entry.

Here is my code:

Controller:

public JsonResult SaveVacation(Vacation model,int empid)
{
        var result = false;

        try
        {
            if (model.Id > 0)
            {
                Vacation vac = db.Vacations.Include(c => c.VacationType).SingleOrDefault(x => x.Id == model.Id);
                vac.EmployeesId = model.EmployeesId;
                vac.VacationType = model.VacationType;
                vac.StartDate = model.StartDate;
                vac.EndDate = model.EndDate;
                vac.Duration = model.Duration;

                db.SaveChanges();

                result = true;
            }
            else
            {
                Vacation vac = new Vacation();
                vac.EmployeesId = model.EmployeesId;
                vac.VacationType = model.VacationType;
                vac.StartDate = model.StartDate;
                vac.Duration = model.Duration;
                vac.EndDate = model.EndDate;
                db.Vacations.Add(vac);
                db.SaveChanges();
                result = true;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return Json(result, JsonRequestBehavior.AllowGet);
}

View

 $("#SaveVacation").click(function () {
            var data = $("#SubmitForm").serialize();
            var empId = getParameterByName('id');
            $.ajax({

                type: "POST",
                url: "http://localhost:26868/Vacations/SaveVacation",
                data: data, empId,
                success: function (result) {
                    alert("succsees");
                    window.location.href = "/Vacations/index";
                    $("#MyModal").modal("hide");
                }
            })
        });

1 Answers

0
K. P. On

First, you are missing curly braces at the data part

Second, the correct version should like like this:

data:{ data:data,id:empId },

These are name/value pairs in ajax

Also, you are missing a semi colon after the success function, should be });

Final version:

 $("#SaveVacation").click(function () {
        var data = $("#SubmitForm").serialize();
        var empId = getParameterByName('id');
        $.ajax({

            type: "POST",
            url: "http://localhost:26868/Vacations/SaveVacation",
            data: { data:data, id:empId },//the name before data and empId can be anything, but there has to be something, because that's what you'll pass to the other site, that's what you'll refer to later
            success: function (result) {
                alert("succsees");
                window.location.href = "/Vacations/index";
                $("#MyModal").modal("hide");
            }
        });
    });