2016-08-21 181 views
1

发送对象名单我有一个像淘汰赛不从后

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; } 

} 

一个C#对象,然后我的HTML是这样的。

  <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> 

然后我的脚本类似于以下

$(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); 

当我得到我的职位从控制器上的呼叫 。

[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 
} 

当我拿到MyModel回,它将拥有所有的值,但儿童会显示有多少孩子有正确的,但数值未发生变化。所有儿童的属性将具有默认值。

有谁知道我如何才能得到正确的水合清单属性?

回答

1

你没有这个选择

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

optionValue所以它会返回

coverageFor: { name: "self" } 

,这是不是在你的服务器端有效,因为它的期待string不反对

将其替换为

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

这是对的钱。谢谢。 – Mhoque