【问题标题】:how to pass a knockout model to jsonresult parameter如何将淘汰模型传递给 jsonresult 参数
【发布时间】:2018-04-18 03:03:45
【问题描述】:

我试图将整个对象传递给 jsonresult 方法。但是会发生错误。这可能是我绑定它的方式,但我不确定。我是 JS 和 KOJS 的新手。单击绑定到 LogUser 方法的 Login 按钮后,它应该调用 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; }
}

这是我的 knockoutjs 视图模型

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

【问题讨论】:

    标签: javascript asp.net-mvc razor knockout.js


    【解决方案1】:

    抛出错误是因为您在EmpObject 之前定义了LoginVm。您需要更改它们的声明顺序。

    您确定这是产生此错误的代码吗?在您看来,您正在绑定thisEmp.EmployeeId(),但错误表明它无法绑定thisEmp().EmployeeId。我想你两个都试过了,错误仍然存​​在。无论哪种方式,都没有必要将 thisEmp 设为 observable。属性是可观察的就足够了。

    所以,将您的代码更改为:

    $(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"})
    

    【讨论】:

    • var LoginVm = {} 和 var LoginVm = function(){} 有什么区别?
    • 在 javascript 中有不同的创建对象的方法。您正在做的是创建键值对的最常见方法。它被称为对象字面量。我创建了一个function 并在其上使用new operator。这也创建了一个对象。您可以通过这种方式在LogUser 方法中访问对象(self) 的实例。
    • var LoginVm = function(){}有没有优缺点?
    • 创建一个函数并对其使用new 关键字,类似于class 在C# 中的工作方式。现在您可以使用new LoginVm() 创建多个LoginVm 对象。我的意思是它在这里没用。但是当你有一个对象表时,这非常有用
    猜你喜欢
    • 2017-12-10
    • 2012-11-18
    • 2017-01-22
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    相关资源
    最近更新 更多