0

我试图从导航栏上的下拉菜单登录,但是当我点击signin按钮时,表单作为 get 方法提交,虽然我定义FormMethod.Post了但它仍然是使用 get 方法提交的,所有值都在QueryString 这里可见是我的代码和图像 在此处输入图像描述

并点击后Signin 在此处输入图像描述

代码:

 @using(Html.BeginForm("Login","Auth",FormMethod.Post,null))
            {
 @Html.TextBoxFor(m=>m.Email,new{@style = "margin-bottom: 15px;",@placeholder = "Email address",@size = "30",@type = "email",@required = ""})

 @Html.PasswordFor(m=>m.Password,new{@style = "margin-bottom: 15px;",@placeholder = "Password",@size = "30"})

 @Html.CheckBoxFor(m => m.RememberMe, new { @style = "float: left; margin-right: 10px;" })
 @Html.LabelFor(m=>m.RememberMe)
 <input class="btn btn-primary" style="clear: left; width: 100%; height: 32px; font-size: 13px;" type="submit" value="Sign In" />
            }

控制器代码

[HttpGet]
    public ActionResult Login(string returl)
    {
        if (!User.Identity.IsAuthenticated)
        {
            ViewBag.returl = returl;
            return View();
        }
        string[] arr1 = System.Web.Security.Roles.GetRolesForUser(User.Identity.Name);
        foreach (var v in arr1)
        {
            if (v.Contains("Administrator"))
            {
                return RedirectToAction("Index", "Admin");
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        return RedirectToAction("Index", "Home");
    }

    [HttpPost]
    public ActionResult Login(Login log, string returl)
    {
        if (ModelState.IsValid && Membership.ValidateUser(log.Email, log.Password))
        {
            if (((MyMembershipProvider)Membership.Provider).IsActive(log.Email))
            {
                FormsAuthentication.SetAuthCookie(log.Email, log.RememberMe);
                string[] arr = System.Web.Security.Roles.GetRolesForUser(log.Email);
                if (returl != null)
                {
                    foreach (var v in arr)
                    {
                        if (v.Contains("Administrator"))
                        {
                            return RedirectToAction("Index", "Admin");
                        }
                        else
                        {
                            return Redirect(returl);
                        }
                    }
                }
                else
                {
                    foreach (var v in arr)
                    {
                        if (v.Contains("Administrator"))
                        {
                            return RedirectToAction("Index", "Admin");
                        }
                        else
                        {
                            return RedirectToAction("Index", "Home");
                        }
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "Please confirm ur email to Login!!!!");
            }
        }
        else
        {
            ModelState.AddModelError("", "Incorrect username or password!!!");
        }
        return View();
    }

解决方案: 导致错误是因为主页包含一个表单标签,在该标签中呈现部分视图,导致它重定向到主页(索引功能)

4

0 回答 0