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
回,它將擁有所有的值,但兒童會顯示有多少孩子有正確的,但數值未發生變化。所有兒童的屬性將具有默認值。
有誰知道我如何才能得到正確的水合清單屬性?
這是對的錢。謝謝。 – Mhoque