-1

-----敲掉js代码------------

var User = function () {
    this.UserName = ko.observable();
    this.Password = ko.observable();
};


var LoginViewModel = function () {
    var self = this;
    this.LoginName = '';
    this.Password = '';
    this.errorvisible = ko.observable(false);

    this.User = ko.observable(new User());
    this.returnurl = '<%: Url.Action("Home","Home") %>';
    this.Login = function () {
        $.ajax({
            url: '../api/LoginApi/',
            contentType: "Application/Json , UTF-8",
            dataType: 'json',
            type: 'POST',
            data: ko.toJson(self.User()),
            success: function (data) {
                var result = data.ID;
                if (result == 400) {
                    self.errorvisible(true);
                } else {
                    self.errorvisible(false);
                    window.location.href = self.returnurl;
                }
            },
            error: function () {
                alert('failed');
            }
        });
    };

};
ko.applyBindings(new LoginViewModel());

------用户模型类------

 public class User
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string UserName { get; set; }
    }


  public void Post(User value)
  {
      //This is the post method  the webAPI which should get the values of properties in User class
  } 

当调试器命中 Post 方法时,用户类实例为 Null(即 Value 为 Null),而不是像 UserName 和 Password 这样的属性中的值

4

1 回答 1

0

您的内容类型contentType: "Application/Json , UTF-8",错误。

它应该是 :

contentType: 'application/json; charset=utf-8'

或者只是简单地:

contentType: 'application/json'

如果没有正确的内容类型,Wep.API 将无法正确处理您的请求,因此您获取null参数。

于 2013-07-17T10:48:07.900 回答