嗨我想成功登录然后调用我的成功函数,否则调用错误函数
在此处查看代码
<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代码:
<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");
}
});
});
});
控制器代码:
[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();
我希望当我们成功登录时,请致电此
toastr['success']("Login Successfully");
当登录失败时,请致电
toastr['error']("Id or Password is incorrect");
请解决这个问题。 提前谢谢!
答案 0 :(得分:0)
假设您的控制器代码命中了返回json的部分,那么您可以通过.Data
访问它:
success: function (data) {
if (data.Data == "login") {
toastr['success']("Login Successfully");
}
else if (data.Data == "error") {
toastr['error']("Id or Password is incorrect");
}
}
您可以在代码中设置.Data
属性:
new JsonResult { Data = message ...
并且成功调用的问题在于它正在测试整个json对象,不是 .Data
属性。
data.toString() == "login"
答案 1 :(得分:0)
不,先生,我们的问题是我们的行动,我们可以把生命中的生命点放在我们这一点首先执行的序列中
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;
}
然后执行此部分
<HorizontalScrollView
android:fillViewport="true"
android:id="@+id/product_hsv"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:measureAllChildren="false"
android:scrollbars="none">
<LinearLayout
android:id="@+id/product_container_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal" />
</HorizontalScrollView>
所以问题是我们无法在消息变量中设置正确的字符串,所以首先你可以在js小提琴中制作一个样本,然后提供链接
答案 2 :(得分:0)
正如您在脚本中使用此代码一样。 :
var dataObject = {
Id: $("#userid").val(),
Password: $("#userpass").val()
};
如果您的LoginMaster
模型包含Id
&amp; Password
它会显示数据。
如果您有UserId
&amp; Password
然后您必须在脚本中更改代码,如下所示。
var dataObject = {
UserId: $("#userid").val(),
Password: $("#userpass").val()
};
简而言之,将Id
替换为UserId
。