mardi 4 août 2015

How to pass NULL value in ajax jquery call?

I have a problem with my Ajax post call. I have two fields one for amount and one for Datetime. I want to be able to pass both values as null values and the program shouldn't crash. My C# method head look like this

public ActionResult Update(int invoiceNumber, DateTime start,decimal payAmount = 0)

And my Jquery code look like this

$(document.body).on("click", ".changeInvoice", function (e) {
        e.preventDefault();
        var invoiceNr = $("#invoiceNumber").val();
        var start = $("#date").val();
        var payAmount = $("#payAmount").val();
        console.log(invoiceNr);

        var updateInvoice =
        {
            invoiceNumber: invoiceNr,
            start: start,
            payAmount: payAmount
        }
        $.ajax({
            type: "POST",
            url: "URL",
            data: updateInvoice,
            success: function (response) {
                location.reload();

            }
        });

If i run the program now its crashes if a user doesn't fill in the start datetime value. But if i set a Datetime? start in my C# code the valdidation works without a crash but then i cant do this in my C# code. As i have understood you cant add days to a nullable datetime variable. Any Suggestions?:

i.FirstReminderDate = start.Date.AddDays(10);

Aucun commentaire:

Enregistrer un commentaire