0

I have a complex object that I need to pass to the controller when submitting a form. This complex object has an object and a list of objects. This is my Web API controller that receives the complex object via post with ajax:

[HttpPost]
public IHttpActionResult CreatePurchaseInvoice(NewPurchaseInvoice newPurchaseInvoice)
{
    try
    {
        var purchaseInvoice = new PurchaseInvoice
        {
            Id = newPurchaseInvoice.PurchaseInvoice.Id,
            DatePurchaseInvoice = newPurchaseInvoice.PurchaseInvoice.DatePurchaseInvoice
        };

        // Here i do other stuff with the list of objects   

        _context.SaveChanges();
    }
    catch(Exception ex)
    {
        return BadRequest();
    }
    return Ok();
 }

This is my html form:

<form id="purchaseInvoiceForm">
    <div class="row">
        <div class="col-lg-6">
            <label>Order:</label>
            <select id="numberOrder" class="form-control" required name="numberOrder">
                <option value="">Select an order number...</option>
            </select>
        </div>
        <div class="col-lg-6">
            <div class="form-group">
                <label>Date of Purchase Invoice:</label><br />
                <input id="datePurchaseInvoice" style="width: 70%" />
            </div>
        </div>
    </div>

    //Here i have an html table and every row i push into an array of the complex object
</form>

And this is my jQuery code where i send the complex object via ajax:

$(document).ready(function(){

    //this is the declaration of my complex object
    var newPurchaseInvoice = {
            PurchaseInvoice: {},
            PurchaseInvoiceDetails: []
    }


    $("#purchaseInvoiceForm").submit(function (e) {
        e.preventDefault();

        newPurchaseInvoice.PurchaseInvoice= {
            Id: $("#numberOrder").val(),
            DatePurchaseInvoice : $("#datePurchaseInvoice").val()
        }

        $.ajax({
            url: "/api/purchaseInvoices",
            method: "post",
            data: newPurchaseInvoice
        });
    });
});

The problem I have is that the date of the KendoDateTimePicker is not sending correctly to the controller. I get this date and not the one I select with the kendoDateTimePicker. This is the DatePurchaseInvoice property of my PurchaseInvoice model in spanish:

enter image description here

This is my KendoDateTimePicker for jQuery:

$("#datePurchaseInvoice").kendoDateTimePicker({        
    value: new Date(),
    dateInput: true
});

And this is my NewPurchaseInvoice model:

public class public class NewPurchaseInvoice 
{
    public PurchaseInvoice PurchaseInvoice{ get; set; }
    public List<PurchaseInvoiceDetail> PurchaseInvoiceDetails{ get; set; }
}

This is my PurchaseInvoice model:

 public class PurchaseInvoice 
{
    public int Id { get; set; }
    public DateTime DatePurchaseInvoice { get; set; }
}
Juan José
  • 193
  • 2
  • 3
  • 22
  • `Date` is not what is getting posted.. `DatePurchaseInvoice` is.. what is the actual property name on `NewPurchaseInvoice` – JamieD77 Oct 25 '17 at 20:57
  • You should post your NewPurchaseInvoice model and PurchaseInvoice model code – JamieD77 Oct 25 '17 at 21:00
  • I edited my post now. Thanks for your reply – Juan José Oct 25 '17 at 21:07
  • What is the actual value your sending, and what is the culture on the server? –  Oct 25 '17 at 21:37
  • the culture that is defined in the bundle class is "~/Scripts/kendo/cultures/kendo.culture.es-ES.min.js", and the value that i want to send is the default value: new Date() or the one that i pick in the kendo widget – Juan José Oct 25 '17 at 21:47
  • Yes but is the culture on the server also `es-ES`? And I mean what is the actual value your sending when it fails - its it all dates, or only when you pick a date with a day greater that 12? –  Oct 26 '17 at 06:01
  • now that you mention it doesn't work in dates with days greater than 12, I don't know why the months it takes as days and vice versa. I had to format it: "MM/dd/yyyy HH:mm" to make it work, I don't know how in other forms I did not have this problem – Juan José Oct 26 '17 at 18:01

1 Answers1

0

You need to be specifying the type of data you are supplying:

contentType: 'application/json'

And possibly dataType too depending on your response type. And according to this post, you may need to stringify your response. I don't think I've needed to do that but I don't often use AJAX operations for complicated data types.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • still does not work with the dates, I think it has to be some problem with kendo. all other data I want to send to the controller are sent correctly but not so with the date. – Juan José Oct 25 '17 at 21:19
  • Oh also yeah I didn't realize, to get the date out of the control, does val() work because you could also use the API: `$("#datePurchaseInvoice").data("kendoDateTimePicker").value()` – Brian Mains Oct 26 '17 at 10:58