jeudi 16 mars 2017

Pass List as parameter to MVC controller

I try to do a thing I do everywhere in my code and for an unknown reason I can't do here.

I try to pass a list of object to my controller but I can't map my array into my list.

View :

var interventions = [
    {
        Id: 1,
        Title: 'Intervention 1',
        ToDoBefore: new Date(),
        PlannedDate: new Date()
    },
    {
        Id: 2,
        Title: 'Intervention 2',
        ToDoBefore: new Date(),
        PlannedDate: new Date()
    },
]

$.ajax({
    url: '/Home/AffectToSubcontractors',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    type: 'POST',
    data: JSON.stringify(interventions),
    success: function (result) {
        debugger;
    }
});

Controller :

    public PartialViewResult AffectToSubcontractors(List<SelectedInterventionsViewModel> interventions)
    {
        // Do something
    }

Model :

public class SelectedInterventionsViewModel
{
    public int Id { get; set; }

    public string Title { get; set; }

    public DateTime ToDoBefore { get; set; }

    public DateTime PlannedDate { get; set; }
}

What I miss ?

Aucun commentaire:

Enregistrer un commentaire