我试图将整个对象传递给jsonresult方法。但是会发生错误。这可能是我绑定的方式,但我不确定。我是JS和KOJS的新手。单击绑定到LogUser方法的“登录”按钮后,它应调用Authenticate(Employee p)方法。

这是我的班级模特

public class Employee
{
    [Key]
    public long AutoId { get; set; }

    [Required]
    Display(Name = "Employee ID")]
    public string EmployeeId { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string EmployeePassword { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
}


这是我的基因敲除模型

$(function () {
    ko.applyBindings(LoginVm);
});

//VIEW MODEL. MODEL IS BELOW THIS
var LoginVm = {

    thisEmp: ko.observable(EmpObject),

    LogUser: function () {
        var self = this;

        //trying to check if thisEmp properties has values by alerting
        alert("ID: " + thisEmp.EmployeeId() + " Password: " + thisEmp.EmployeePassword());

        $.ajax({
            url: '/Employee/AuthenticateUser',
            type: 'POST',
            dataType: 'json',
            data: ko.toJSON(thisEmp),
            contentType: 'application/json',
            success: function (errorMsg) {
                if (errorMsg === '') {

                }
            }
        });


    }
};

//MODEL
var EmpObject = {
    EmployeeId: ko.observable(''),
    EmployeePassword: ko.observable('')
}


这是我的观点以及如何绑定属性

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Employee</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.EmployeeId)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.EmployeeId, new { data_bind="value: thisEmp.EmployeeId()"})
            @Html.ValidationMessageFor(model => model.EmployeeId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.EmployeePassword)
        </div>
        <div class="editor-field">
            @Html.PasswordFor(model => model.EmployeePassword, new { data_bind="value: thisEmp.EmployeePassword()"})
            @Html.ValidationMessageFor(model => model.EmployeePassword)
        </div>B

        <p>
            @*<input type="submit" value="Create"/>*@
            <input type="button" value="Login" data-bind="click: LogUser"/>
        </p>
    </fieldset>
}


这是错误

Uncaught TypeError: Unable to process binding "value: function (){return thisEmp().EmployeeId }"
Message: Cannot read property 'EmployeeId' of undefined
    at value (eval at createBindingsStringEvaluator

最佳答案

因为在LoginVm之前定义了EmpObject,所以引发了错误。您需要更改它们的声明顺序。

您确定这是导致此错误的代码吗?在您看来,您正在绑定thisEmp.EmployeeId(),但是错误表明它无法绑定thisEmp().EmployeeId。我认为您同时尝试了这两个方法,但错误仍然存​​在。无论哪种方式,都无需使thisEmp成为可观察的。这些属性是可观察的就足够了。

因此,将您的代码更改为:

$(function () {
    ko.applyBindings(new LoginVm());
});

//MODEL
var EmpObject = {
    EmployeeId: ko.observable(''),
    EmployeePassword: ko.observable('')
}

//VIEW MODEL. MODEL IS BELOW THIS
var LoginVm = function() {
    var self = this;

    self.thisEmp = EmpObject;

    self.LogUser = function () {
        var self = this;

        //trying to check if thisEmp properties has values by alerting
        alert("ID: " + self.thisEmp.EmployeeId() + " Password: " + self.thisEmp.EmployeePassword());

        $.ajax({
            url: '/Employee/AuthenticateUser',
            type: 'POST',
            dataType: 'json',
            data: ko.toJSON(self.thisEmp),
            contentType: 'application/json',
            success: function (errorMsg) {
                if (errorMsg === '') {

                }
            }
        });
    }
};


并将绑定更改为:

@Html.TextBoxFor(model => model.EmployeeId, new { data_bind="value: thisEmp.EmployeeId"})
@Html.PasswordFor(model => model.EmployeePassword, new { data_bind="value: thisEmp.EmployeePassword"})

关于javascript - 如何将 knockout 模型传递给j​​sonresult参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47128670/

10-13 06:23