值'[object HTMLInputElement]'无效

时间:2013-06-18 19:01:48

标签: asp.net-mvc knockout.js

提交表单时出现以下错误:

The value '[object HTMLInputElement]' is not valid for MaritalStatus

这是模特:

public class CompanionsModel
{
        public char Gender { get; set; }
        public int MaritalStatus { get; set; }
}

这是观点:

<input type="hidden" id="MaritalStatus" name="MaritalStatus" data-bind="value: MaritalStatus" value="@Model.MaritalStatus" />
    @Html.HiddenFor(m => m.Gender, new {data_bind="value: StudentGender" })

这是knockoutsjs代码:

 function CompanionController() {
            var self = this;
            var GenderValue = $("[name=Gender]").val();
            var MaritalStatusValue = $("#MaritalStatus").val();

            self.StudentGender = ko.observable(GenderValue); 

            self.StudentMaritalStatus = ko.observable(MaritalStatusValue);
}

1 个答案:

答案 0 :(得分:5)

错误是因为'MaritalStatus'复选框中的无效Knockouts数据绑定

正确的绑定是' StudentMaritalStatus 'NOT'MaritalStatus'

<input type="hidden" id="MaritalStatus" name="MaritalStatus" data-bind="value: StudentMaritalStatus" value="@Model.MaritalStatus" />