私はMVCノックアウトが初めてです。誰か助けてください。
ノックアウト js を使用して ajax 呼び出しで MVC4 webapi アクション メソッドを呼び出しています。ログインボタンをクリックすると、WebApi アクションメソッドが呼び出されます。 View にエラーメッセージを表示するにはどうすればよいですか?
var User = function () {
this.Email = ko.observable();
this.Password = ko.observable();
};
var LoginViewModel = function () {
var self = this;
this.IncorrectLogin = ko.observable(false);
this.User = ko.observable(new User());
this.Login = function () {
$.ajax({
url: '../api/Login/PostAddUser',
contentType: "application/json",
cache: false,
dataType: 'json',
type: 'POST',
data: "{'Email':'" + self.User.Email + "' , 'password':'" + self.User.Password + "'}",
success: function (data,status,xhttps) {
var result = data;
if (data == true) {
window.location.href = '../User/Account';
} else {
}
},
error: function (data, status, xhttps) {
alert('failed');
}
});
};
};
ko.applyBindings(new LoginViewModel());
したがって、そのユーザーIDとパスワードが有効でない場合、ビューにメッセージを表示したいと思います。コントローラーからビューにエラーメッセージを渡すにはどうすればよいですか?
public bool PostAddUser(User user)
{
User u = DataAccessLayer.ValidateUser(User.email, User.password);
if (string.IsNullOrEmpty(u.Id) || u.Id == 0)
{
//retun the custome validation error like Invalid UserId
}
else
{
if (u.activeCodeConfirmed)
{
return true;
}
else
{
return false;
}
}
}