2013-05-13 53 views
0

我在这里看到的问题和答案有点类似于我的问题,但它们要么比我的实现更先进,要么是不同的方向。在knockout.js中嵌套的viewmodels和json

的事情是,接收具有嵌套信息像这样一个JSON字符串:

{"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"} 
} 

我可以重新包装这一个视图模型,我能理解(我击倒的技能是非常基本的,我刚学这个),但问题是当我必须发送视图模型回到后端。这是一个网络API。 web api期望返回相同类型的json。

这是我目前的视图模型:

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("") 
    }; 

就像我说的,简单的。但问题是如何复制嵌套。这样做的观像这样的视图模型不能正常工作:

StudentBaseData.firstname: ko.observable(""), 
    StudentBaseData.lastname: ko.observable(""), 
    StudentBaseData.email: ko.observable(""), 

无论是做这个的:

"StudentBaseData.firstname": ko.observable(""), 
    "StudentBaseData.lastname": ko.observable(""), 
    "StudentBaseData.email": ko.observable(""), 

然后,我看到是这样的:

StudentBaseData[ 
lastname: ko.observable(""), 
email": ko.observable("") 
] 

,要么不工作。

我该怎么办?

回答

2

这应该工作:

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("") 
    } 
}; 

在你的HTML:

<span data-bind="text: PrimaryEducation.educationName"></span> 
+0

太好了!快速回答,但在我实现它之前,我如何以编程方式分配值?通常它的“ViewModel.firstname(”new value“)”。 – 2013-05-13 08:05:47

+0

'ViewModel.PrimaryEducation.educationInstitution(“great”);' – Catalin 2013-05-13 08:08:32

+0

好吧,它需要一段时间来实现和返工页面,但它的工作原理!太棒了! :D 仍需要整理一下,但这是一个巨大的进步,完美!非常感谢:) – 2013-05-13 08:36:43