2016-08-21 1 views
1

Senden habe ich ein C# Objekt wieKnockout nicht Liste des Objekts von Post

public class MyModel 
{ 
    public string ZipCode { get; set; } 
    public DateTime DateOfEvent { get; set; } 

    public List<Children> Applicants { get; set; } 

    public string SelectedChoice { get; set; } 

} 

public class Children 
{ 
    public string CoverageFor { get; set; } 
    public DateTime dateOfBirth { get; set; } 

} 

dann mein html so ist.

  <div class="control-group span4"> 
       <label class="control-label" for="4">County</label><select name="county" id="4" data-bind="options: county, optionsText:'County', value:selectedCounty, optionsValue: 'FIPSCountyCode' , optionsCaption: 'Choose...'"></select> 

      </div> 
     </div> 
@*--------------------------------------- some more things --------------*@ 
      <div class="row-fluid dependent " data-bind='foreach: applicants'> 
         <div class="control-group span4"> 
         <label class="control-label" for="5"> 
          Coverage 
          for 
         </label> 
         <select name="coverageFor" id="5" data-bind="options: coverages, value:coverageFor, optionsText:'name', optionsCaption: 'Choose...'"></select> 
        </div> 
        <div class="control-group span4"> 
         <label class="control-label" for="6"> 
          Date of 
          Birth 
         </label> 

         <input id="6" placeholder="MM/DD/YYYY" class="input-small" data-date-blur="true" data-regex="^((\d{0,2})|(\d{1,2}/?\d{0,2})|(\d{1,2}/?\d{1,2}/?\d{0,4}))$" 
           type="text" data-bind="value: dateofBirth"/> 



        </div> 
        @*<a href='#' data-bind='click: $parent.removeApplicant, enable: $root.applicants().length > 1'>Remove</a>*@ 
        <!-- ko if: $index() != 0 --> 
        <a href='#' data-bind="click: $root.removeApplicant">Remove</a> 
        <!-- /ko --> 

      </div> 

Dann mein Skript ist wie folgt

$(function() { 
    viewModel.zipCode.subscribe(function (value) { 
     $.get("/api/xx/GetCounties?zipCode=" + value, null, viewModel.county, 'json'); 
    }.bind(this)); 
}); 


var ViewModel = function() { 
    var self = this; 

    self.zipCode = ko.observable(); 
    self.dateOfEvent = ko.observable(); 
    self.isValidZip = ko.observable(false); 
    self.selectedChoice = ko.observable(); 


    //Enrollment reasons 
    self.enrollmentReasons = ko.observableArray(new Array()); 
    $.get("/api/myApi/GetEnrollmentReason", null, self.enrollmentReasons, 'json'); 

    //county from Zipcode subscribed 
    self.county = ko.observableArray(new Array()); 
    $.get("/api/myApi/GetCounties?zipCode=" + self.zipCode, null, self.county, 'json'); 


    self.applicants = ko.observableArray(new Array()); 
    self.applicants.push(new Children()); 

    //$.get("/api/myApi/IsInServiceArea?zipCode=" + value, null, ViewModel.isValidZip, 'json'); 


    //operations 
    self.addApplicant = function() { self.applicants.push(new Children()) }; 
    self.removeApplicant = function (Children) { self.applicants.remove(getaquoteapplicant) }; 

    self.save = function() { 
     var m = ko.mapping.toJS(ko.utils.unwrapObservable(self)); 
     $.ajax({ 
      url: '/mypostingpage/start', 
      type: 'POST', 
      contentType: 'application/json; charset=utf-8', 
      data: JSON.stringify(m), 
      success: function (result) { 
       alert(result); 

      } 
     }); 
    } 

} 

var Children= function() { 
    var self = this; 
    self.coverageFor = ko.observable(); 
    self.tobaccoUser = ko.observable(); 
    self.dateofBirth = ko.observable(); 

    self.coverages = ko.observableArray([ 
     { name: "Self" }, 
     { name: "Spouse/Life Partner" }, 
     { name: "Son" }, 
     { name: "Daughter" }, 
     { name: "Sibling" }, 
     { name: "Other Eligible Person" } 
    ]); 


}; 

viewModel = new ViewModel(); 

ko.applyBindings(viewModel); 

Als ich meinen Beitrag aus dem Aufruf auf dem Controller erhalten.

[System.Web.Mvc.HttpPost] 
public ActionResult Start(MyModel model) <------- has all the values but the Children's property values not in. 
{ 

    //Stuff I will return 
} 

Wenn ich die MyModel zurück, wird es alle Werte, aber die Kinder werden zeigen, wie viele Kinder ich habe richtig, aber die Werte nicht ändern. Alle Eigenschaften für Kinder haben die Standardwerte.

Weiß jemand, wie ich die List-Eigenschaften richtig hydratisieren kann?

Antwort

1

Sie nicht optionValue auf dieser ausgewählten

<select name="coverageFor" id="5" data-bind="options: coverages, value:coverageFor, optionsText:'name', optionsCaption: 'Choose...'"></select> 

erwähnt hat, so wird er zurückkehren

coverageFor: { name: "self" } 

und das ist auf Ihrem Server-Seite nicht gültig, da es ein string nicht

Objekt erwartet

ersetzen Sie es durch

<select name="coverageFor" id="5" data-bind="options: coverages, value:coverageFor, optionsText:'name', optionsValue:'name', optionsCaption: 'Choose...'"></select> 
+0

Das ist richtig auf das Geld. Vielen Dank. – Mhoque

Verwandte Themen