簡體   English   中英

嵌套的viewmodels和json在knockout.js中

[英]Nested viewmodels and json in knockout.js

我在這里看到的問題和答案在某種程度上類似於我的問題,但它們要么比我的實現更先進,要么是一個不同的方向。

問題是,接收一個具有嵌套信息的json字符串,如下所示:

{"StudentBaseData":{
    "StudentGuid":123456,
    "FirstName":"my name",
    "LastName":"my last name",
    "Email":"email@email.com",
    "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"}
}

我可以重新打包到一個我能理解的視圖模型(我的淘汰技能非常基本,我只是學習這個),但問題是我必須將視圖模型發送回后端。 這是一個web api。 web api期望返回相同類型的json。

這是我當前的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("")
    };

就像我說的那樣,簡單。 但問題是如何復制嵌套。 在viewmodel中執行這樣的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("")
]

這也不起作用。

我該怎么辦?

這應該工作:

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM