hi i am want to when login succesfully then call my success function otherwise call error function
View code here
<div class="container">
<div class="login-container">
<div class="avatar"><img src="@Url.Content("~/Content/images/download.jpeg")" style="max-width:95%;" /></div>
<div class="form-box">
@using (Html.BeginForm())
{
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
@Html.TextBoxFor(m => m.UserId, new { @class = "form-control", @id="userid", @placeholder = "Username", @required = "required", @maxlength = "20" })
</div>
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
@Html.PasswordFor(m => m.Password, new { @class = "form-control", @id = "userpass", @placeholder = "Password", @required = "required", @maxlength = "20" })
</div>
<button class="btn btn-info btn-block login" type="submit" id="login-btn"><i class="glyphicon glyphicon-log-in"></i> Login</button>
}
</div>
</div>
ajax code here:
<script>
$(document).ready(function () {
$('#login-btn').click(function () {
var dataObject = {
Id: $("#userid").val(),
Password: $("#userpass").val()
};
$.ajax({
url: '@Url.Action("Login","Account")',
type: "POST",
data: dataObject,
dataType: "json",
success: function (data) {
if (data.toString() == "login") {
toastr['success']("Login Successfully");
}
else if (data.toString() == "error") {
toastr['error']("Id or Password is incorrect");
}
},
error: function () {
toastr['error']("Hello");
}
});
});
});
Controller Code here:
[HttpPost]
public ActionResult Login(LoginMaster model)
{
string message = "";
if (ModelState.IsValid)
{
try
{
var user = from emp in db.LoginMasters
where emp.UserId == model.UserId && emp.Password == model.Password
select emp;
var rol = user.FirstOrDefault();
if (rol != null)
{
var realrol = rol.Role;
if (realrol == "admin")
{
message = "login";
return RedirectToAction("Index", "Home");
}
else if (realrol == "user")
{
Session["userid"] = rol.UserId;
message = "login";
return RedirectToAction("User", "Home");
}
}
else
{
message = "error";
}
}
catch (Exception ex)
{
ViewBag.cath = ex.Message;
}
}
else
{
message = "error";
}
if (Request.IsAjaxRequest())
{
return new JsonResult { Data = message, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
return View();
i am want to when we login succesfully that time call this
toastr['success']("Login Successfully");
and when login fail that time call
toastr['error']("Id or Password is incorrect");
please solve this problem. thanks in advance!
Assuming your controller code hits the part that returns the json then you can access it via .Data
:
success: function (data) {
if (data.Data == "login") {
toastr['success']("Login Successfully");
}
else if (data.Data == "error") {
toastr['error']("Id or Password is incorrect");
}
}
You set the .Data
property within your code here:
new JsonResult { Data = message ...
and the problem with your success call is that it is testing the entire json object, not the .Data
property.
data.toString() == "login"
no sir our problem is our Action where we can put hit point that is not run in a sequence our this point is firstly execute
if (Request.IsAjaxRequest())
{
return new JsonResult { Data = message, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
then after execute this part
try
{
var user = from emp in db.LoginMasters
where emp.UserId == model.UserId && emp.Password == model.Password
select emp;
var rol = user.FirstOrDefault();
if (rol != null)
{
var realrol = rol.Role;
if (realrol == "admin")
{
message = "login";
return RedirectToAction("Index", "Home");
}
else if (realrol == "user")
{
Session["userid"] = rol.UserId;
message = "login";
return RedirectToAction("User", "Home");
}
}
else
{
message = "error";
}
}
catch (Exception ex)
{
ViewBag.cath = ex.Message;
}
so problem is we can not set correct string in message variable so please first of all you can make a sample in js fiddle then provide me link
As you have used this code in your script. :
var dataObject = {
Id: $("#userid").val(),
Password: $("#userpass").val()
};
if your LoginMaster
model contains Id
& Password
it'll display data. If you have UserId
& Password
then you have to change your code in script like below.
var dataObject = {
UserId: $("#userid").val(),
Password: $("#userpass").val()
};
In short replace Id
with UserId
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.