I’m currently working in asp.net mvc 4. I’ve written a function which return a JSON-object.
This is the function (in my controller)
[HttpPost]
public ActionResult Login(string email, string password)
{
ViewData["Message"] = "None";
JSONLoginModel model = new JSONLoginModel();
Account account = _accountRepository.CheckLogin2(email, password);
if (account != null)
{
model.Email = email;
model.Password = password;
model.ChangePassword = account.ChangePasswordOnLogin;
}
return Json(model);
}
And this is the JSONLoginModel
[Serializable]
public class JSONLoginModel
{
public string Email { get; set; }
public string Password { get; set; }
public bool ChangePassword { get; set; }
}
I’ve also written the following JQuery code to catch and use it
$.post("Home/Login", { email: email, password: password }, function (data) {
alert(data);
$.each(data, function (index, IntraNoviUser) {
if (IntraNoviUser !== undefined && IntraNoviUser !== null) {
alert('test1');
alert(IntraNoviUser.password, IntraNoviUser.email);
alert('test2');
}
});
});
Everything goes fine until I try to use the result that was returned.
What I get back from my controller is an object which can have 3 options:
- null
- email + password filled in and
changePasswordis false - same as 2, but
changePassword = true
The problem is that my returned JSON object is never recognized.
Any hint on this?
I Think asp.net resturns json data as d. So you better alert(data.d) and check it.