在mvc4中没有回发登录表单的onclick [英] onclick without postback login form in mvc4

查看:77
本文介绍了在mvc4中没有回发登录表单的onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用mvc4做一个登录页面。



我到现在为止:



成功登录后退。



但我需要:



使用ajax成功登录后无需回传jQuery





这是我的代码:

index.cshtml

< br $> b $ b

I'm doing a login page in mvc4 .

I did till now:

Successfully login with post back.

But i need :

Successfully login without post back using ajax and jQuery


Here is my code :
index.cshtml


<script src="jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript">
    function ValidateUser() {
        var userid = $("#<%=txtuserid.ClientID %>").attr('value');
        var password = $("#<%=txtpassword.ClientID %>").attr('value');
        $("#btnSubmit").attr({ 'value': 'Please wait...' });
        $.ajax({
            url: "Home/Index",
            type: "POST",
            cache: false,
            success: function (html) {
                if (html == "1") {
                    alert("Successfull login.");
                } else {
                    alert("Wrong User Id and Password.");
                }

                $("#btnSubmit").attr({ 'value': 'Submit' });
                return false;
            }
        });
    }
</script>












@using(Html.BeginForm(Index,Home,FormMethod.Post,new {id =ID}))

{

@ Html.AntiForgeryToken()//这是为了防止CSRF攻击

@ Html.ValidationSummary(true)

if(@ViewBag .Message!= null)

{


@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "ID" }))
{
@Html.AntiForgeryToken() // this is for prevent CSRF attack
@Html.ValidationSummary(true)
if (@ViewBag.Message != null)
{


@ ViewBag.Message


@ViewBag.Message



}


}



登录


Login


@ Html.LabelFor(m => m.EmailId)


@Html.LabelFor(m => m.EmailId)






@ Html.TextBoxFor(m => ; m.EmailId,new {id =txtuserid})

@ Html.ValidationMessageFor(m => m.EmailId)


@Html.TextBoxFor(m => m.EmailId, new { id = "txtuserid" })
@Html.ValidationMessageFor(m => m.EmailId)






@ Html.LabelFor(m => m.Password)


@Html.LabelFor(m => m.Password)






@ Html.TextBoxFor(m => m.Password,new {id =txtpassword})

@ Html.ValidationMessageFor(m => m.Password)


@Html.TextBoxFor(m => m.Password, new { id = "txtpassword" })
@Html.ValidationMessageFor(m => m.Password)



< input type =submitvalue =Submitid =btnloginonclick =javascript:return ValidateUser() ; />


<input type="submit" value="Submit" id="btnlogin" onclick="javascript:return ValidateUser();" />



}


}









HomeController.cs









HomeController.cs


[HttpPost]
      [ValidateAntiForgeryToken]
       public ActionResult Index(LoginMVC u)
       {

           if (ModelState.IsValid) // this is check validity
           {
               using (LoginAndSignUpEntities dc = new LoginAndSignUpEntities())
               {
                   var v = dc.tbl_Detailstbl.Where(a => a.Email_Id.Equals(u.EmailId) && a.Password.Equals(u.Password)).FirstOrDefault();
                   if (v != null)
                   {
                       Session["LogedUserID"] = v.Email_Id.ToString();
                       Session["LogedUserFullname"] = v.Name.ToString();
                       return RedirectToAction("AfterLogin");
                   }
               }
           }
           return View(u);
       }
      public ActionResult AfterLogin()
      {
          if (Session["LogedUserID"] != null)
          {
              return View();
          }
          else
          {
              return RedirectToAction("Index");
          }
      }









请帮我.....





Please help me.....

推荐答案

#<%= txtuserid.ClientID%>)。attr(' value');
var password =
("#<%=txtuserid.ClientID %>").attr('value'); var password =


#<%= txtpassword.ClientID%>)。attr(' value');
("#<%=txtpassword.ClientID %>").attr('value');


#btnSubmit )。attr({' value'' 请稍候......'});
("#btnSubmit").attr({ 'value': 'Please wait...' });


这篇关于在mvc4中没有回发登录表单的onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆