Sto avendo uno strano problema con MVC3 e l'associazione del modello. Quando inserisco un oggetto JSON sul mio controller, il raccoglitore modello non può creare affatto un oggetto digitato. Tutte le proprietà sono predefinite (vale a dire stringhe vuote)MVC3 Il binding del modello Json non funziona quando viene inviato al server
Tuttavia, se creo un'istanza sul server e la invio come risultato dell'azione JSON, i dati sul cavo appaiono identici.
Ho provato con
$.ajaxSettings.traditional = true;
e non fa alcuna differenza
Per fare un esempio, se io inserisco
{"RoutineName":"My new routine","Routines":[{"DayName":"Monday","Items":[21,31]}]}
Il modello legante fallisce, ma proveniente dal server i dati assomiglia a
{"RoutineName":"Routine From Code","Routines":[{"DayName":"Monday","Items":[1,2]},{"DayName":"Tuesday","Items":[]}]}
.210
il codice HTML utilizzato per generare questo assomiglia
$('#submitRoutine').click(function() {
var routines = [];
$('.DayName').each(function (index, item) {
var $item = $(item);
var name = $item.html();
var routineItems = [];
$($item.attr('href')).find('.itemId').each(function() {
routineItems.push(parseInt($(this).val(), 10));
});
routines.push({
DayName: name,
Items: routineItems
});
});
var routine = {
RoutineName: $('#routineName').val(),
Routines: routines
};
$.ajaxSettings.traditional = true;
$.post('/Machine/CreateRoutine', JSON.stringify(routine),function (data) {},'json');
});
Quindi sembra che il modello vincolante da un oggetto tipizzato per JSON è ok, ma tornando dall'altra parte non lo è. C'è qualcosa che ho perso?
I modelli sono in F #
type RoutineDayViewModel() =
let mutable _name = String.Empty
let mutable _items = new ResizeArray<int>()
member x.DayName with get() = _name and set value = _name <- value
member x.Items with get() = _items and set value = _items <- value
type RoutineViewModel() =
let mutable _name = String.Empty
let mutable _routines = new ResizeArray<RoutineDayViewModel>()
member x.RoutineName with get() = _name and set value = _name <- value
member x.Routines with get() = _routines and set value = _routines <- value
EDIT: Ho anche provato con le seguenti classi C# e ottenere lo stesso risultato
public class RoutineDayViewModel
{
public string DayName { get; set; }
public List<int> Items{ get; set; }
}
public class RoutineViewModel
{
public string RoutineName { get; set; }
public List<RoutineDayViewModel> Routines { get; set; }
}
Ho anche aggiunto quanto segue al global.asax
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory())
Grazie
Che aspetto ha il tuo modello? Stai usando getter e setter giusto? – JesseBuesking
sì, sono modelli F # ma hanno getter e setter proprio come farebbe una classe C#. Ho aggiunto il codice del modello alla mia domanda. ResizeArray è un nome F # per la lista –
Dylan
So che in C# devi dichiarare esplicitamente che un controller consente le richieste JSON GET altrimenti le rifiuterà. L'hai fatto per i tuoi controller?* Il mio male stai postando>< – JesseBuesking