Reputation: 553
I have seen questions and answers here that somewhat resembles my question here, but they are either way more advanced than my implementation, or is a different direction.
The thing is, a receive a json string that has nested information like so:
{"StudentBaseData":{
"StudentGuid":123456,
"FirstName":"my name",
"LastName":"my last name",
"Email":"[email protected]",
"Password":123456,
"Birthdate":"01-01-1986",
"Picture":null,
"MobilePhone":"123456789",
"Gender":"Hr."},
"PrimaryEducation":{
"Name":"something",
"Institution":"something",
"StudyStartDate":"2011-12-01",
"GraduationDate":"2013-12-01",
"ThesisSubject":"something"},
"MAddress":{
"Street":"a road",
"StreetNr":"12",
"ZipCode":"1234",
"City":"a city"}
}
I can repackage this to a viewmodel that i can understand (my knockout skills are very basic, i am just learning this), but the problem is when i have to send the viewmodel back to the backend. Which is a web api. The web api expects the same type of json to be returned.
This is my current viewmodel:
var ViewModel = {
studentGuid: ko.observable("<%=Session["guid"]%>"),
firstname: ko.observable(""),
lastname: ko.observable(""),
email: ko.observable(""),
password: ko.observable(""),
birthdate: ko.observable(""),
day: ko.observable(""),
month: ko.observable(""),
year: ko.observable(""),
picture: ko.observable(""),
mobilephone: ko.observable(""),
gender: ko.observable(""),
street: ko.observable(""),
streetnr: ko.observable(""),
zipcode: ko.observable(""),
city: ko.observable(""),
primaryEducationName: ko.observable(""),
primaryEducationInstitution: ko.observable(""),
primaryEducationStudyStartDate: ko.observable(""),
primaryEducationGraduationDate: ko.observable(""),
primaryEducationThesisSubject: ko.observable("")
};
Like i said, simple. But the problem is how to replicate the nesting. Doing the observables like so in the viewmodel does not work:
StudentBaseData.firstname: ko.observable(""),
StudentBaseData.lastname: ko.observable(""),
StudentBaseData.email: ko.observable(""),
Neither does this:
"StudentBaseData.firstname": ko.observable(""),
"StudentBaseData.lastname": ko.observable(""),
"StudentBaseData.email": ko.observable(""),
And then i have seen something like:
StudentBaseData[
lastname: ko.observable(""),
email": ko.observable("")
]
That doesnt work either.
What should i do?
Upvotes: 0
Views: 1034
Reputation: 11721
This should work:
var ViewModel = {
StudentBaseData: {
studentGuid: ko.observable("<%=Session["guid"]%>"),
firstname: ko.observable(""),
lastname: ko.observable(""),
email: ko.observable(""),
password: ko.observable(""),
birthdate: ko.observable(""),
day: ko.observable(""),
month: ko.observable(""),
year: ko.observable(""),
picture: ko.observable(""),
mobilephone: ko.observable(""),
gender: ko.observable(""),
},
MAddress: {
street: ko.observable(""),
streetnr: ko.observable(""),
zipcode: ko.observable(""),
city: ko.observable(""),
},
PrimaryEducation: {
educationName: ko.observable(""),
educationInstitution: ko.observable(""),
educationStudyStartDate: ko.observable(""),
educationGraduationDate: ko.observable(""),
educationThesisSubject: ko.observable("")
}
};
In your html:
<span data-bind="text: PrimaryEducation.educationName"></span>
Upvotes: 2