使用MVC 4,我使用knockout(在剃刀视图中)填充变量,我想通过我的viewmodel将该变量传递回我的控制器。例如
var selectedProgramOptions;
$.ajax({
type: "GET",
contentType: "application/json",
dataType: "jsonp",
url: ceraSvcUrl + "/api/Registrations/GetRegistrationOptions?regId=@Model.Registration.Id",
success: function (result) {
selectedProgramOptions = result;
$.ajax({
type: "GET",
contentType: "application/json",
dataType: "jsonp",
url: ceraSvcUrl + "/api/Programs/GetProgramOptions?programId=@Model.Registration.Program.Id",
success: function (allProgsResult) {
if (allProgsResult.length == 0)
$('#programOptionsDiv').hide();
for (var i = 0; i < allProgsResult.length; i++) {
var match = false;
for (var j = 0; j < selectedProgramOptions.length; j++) {
if (allProgsResult[i].Name == selectedProgramOptions[j].Name) {
match = true;
self.AvailableOptions.push(new ProgramOption(selectedProgramOptions[j].Name, selectedProgramOptions[j].ChargeCode, selectedProgramOptions[j].Price, true, selectedProgramOptions[j].Notes, selectedProgramOptions[j].OptionGroup));
}
}
var chgCode = allProgsResult[i].ChargeCode;
if (match == false && ["TG", "TN", "TP", "TV", "TW", "E2", "E3", "L2", "L3"].indexOf(chgCode) <= -1)
self.AvailableOptions.push(new ProgramOption(allProgsResult[i].Name, allProgsResult[i].ChargeCode, allProgsResult[i].Price, false, allProgsResult[i].Notes, allProgsResult[i].OptionGroup));
}
}
});
}
});
这是填充knockout字段的代码是self.AvailableOptions。我需要将该字段放入@ Model.Registration.AvailableOptions。