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

デバッガーがポスト メソッドをヒットすると、UserName や Password などのプロパティの値ではなく、User クラス インスタンスが Null (つまり、Value が Null) になります。

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 に答える