淘汰赛没有从post发送对象列表

本文关键字:对象 列表 post 淘汰赛 | 更新日期: 2023-09-27 18:03:23

我有一个c#对象,像

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

然后我的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返回时,它将拥有所有的值,但是Children将显示我有多少个子,但是值没有改变。所有子属性都将具有默认值。

有谁知道我如何才能得到列表属性水合适当?

淘汰赛没有从post发送对象列表

你没有提到optionValue在这个select

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

所以它会返回

coverageFor: { name: "self" }

,这在你的服务器端是无效的,因为它期望一个string不是对象

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