function UserLogin() {
var username = $("#txtUsername").val();
var passcode = $("#txtPassword").val();
alert(username);
$.ajax({
url: '@Url.Action("Login", "UserAccount")',
type: "POST",
data: { 'username': username, 'passcode': passcode },
datatype: "json",
traditional: true,
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data.username);
}
});
[HttpPost] public ActionResult Login(string username, string passcode) { if (IsValidUser(username, passcode)) { return RedirectToAction("UserInfo", "UserAccount"); } else { ModelState.AddModelError("", "Your Username or password is invalid"); } return View(); }
private bool IsValidUser(string username, string passcode)
{
string Query = "Select Count(ID) from Users where Username =" username " and PassCode = " passcode "";
int count = Convert.ToInt32(NpgSQLHelper.ExecuteScalar(Utility._connectionstring, System.Data.CommandType.Text, Query));
if (count > 0)
{
return true;
}
else
{
return false;
}
}
CodePudding user response:
Remove the contentType: "application/json; charset=utf-8"
or modify contentType to contentType:"application/x-www-form-urlencoded"
can work fine.