我有一个用户登录页面,我想检查插入的凭据是否正确。 这是控制器:
编辑:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using SustIMS.Models; namespace SustIMS.Controllers { public class MainController : Controller { public static string userName; public static string password; public ActionResult Index() { getCredentials(); if (Authenticate(userName, password)) { return View(); } ModelState.AddModelError("", "Could not authenticate"); return Redirect("../Home"); } public void getCredentials() { if (Request["username"] != null && Request["password"] != null) { userName = Request["username"].ToString(); password = Request["password"].ToString(); } } } }如果插入的凭据正确,则一切正常: Authenticate函数验证该函数,如果它返回true ,则ActionResult返回View 。
但是,如果它们不正确,我将其设置为返回null ,而是转到空白页面。
我希望它保持在同一页面并向用户显示某种消息,通知凭据不正确(通过显示div ,使用弹出窗口调用javascript函数,...,我不是真的知道)。
我怎样才能做到这一点?
谢谢
编辑
这是我的观点:
@{ Layout = null; } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>@ViewBag.Message</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") @Scripts.Render("~/bundles/jquery") </head> <body> <div id="outer-limit"> <div class="logo-main"> <a href="../Home/Index"> <img class="displayed" src="../../Images/logo.png" alt="SustIMS" /></a> </div> <section class="container"> @Html.Partial("LoginForm") @Html.ValidationSummary() </section> </div> </body> </html>和部分LoginForm :
<div class="login"> <h1>@ViewBag.Login</h1> <form method="get" action="../Main/Index"> <p> <input type="text" name="username" value="" placeholder="Username" maxlength="30"></p> <p> <input type="password" name="password" value="" placeholder="Password" maxlength="25"></p> <br /> <br /> <p class="submit"> <input type="submit" onclick="Refresh()" name="commit" value="Login"></p> </form> </div>I've got a user login page that I want to check if the inserted credentials are correct. Here's the controller:
EDITED:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using SustIMS.Models; namespace SustIMS.Controllers { public class MainController : Controller { public static string userName; public static string password; public ActionResult Index() { getCredentials(); if (Authenticate(userName, password)) { return View(); } ModelState.AddModelError("", "Could not authenticate"); return Redirect("../Home"); } public void getCredentials() { if (Request["username"] != null && Request["password"] != null) { userName = Request["username"].ToString(); password = Request["password"].ToString(); } } } }Everything is OK if the inserted credentials are correct: the Authenticate function verifies that and if it returns true, the ActionResult returns the View.
However, if they are not correct, I set it to return null, going to a blank page instead.
I want it to stay on the same page and display some kind of message to the user informing that the credentials aren't correct (by showing a div, calling a javascript function with a popup window, ..., I don't really know).
How can I do that?
Thanks
EDIT
Here's my view:
@{ Layout = null; } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>@ViewBag.Message</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") @Scripts.Render("~/bundles/jquery") </head> <body> <div id="outer-limit"> <div class="logo-main"> <a href="../Home/Index"> <img class="displayed" src="../../Images/logo.png" alt="SustIMS" /></a> </div> <section class="container"> @Html.Partial("LoginForm") @Html.ValidationSummary() </section> </div> </body> </html>And the partial LoginForm:
<div class="login"> <h1>@ViewBag.Login</h1> <form method="get" action="../Main/Index"> <p> <input type="text" name="username" value="" placeholder="Username" maxlength="30"></p> <p> <input type="password" name="password" value="" placeholder="Password" maxlength="25"></p> <br /> <br /> <p class="submit"> <input type="submit" onclick="Refresh()" name="commit" value="Login"></p> </form> </div>最满意答案
一种方法:
public ActionResult Index(string url) { getCredentials(); if (Authenticate(userName, password)) { return View(); } else { TempData["ErrorMessage"]="Wrong Credential"; return Redirect(url); } } }然后在您的视图中显示div中的消息,如果它不为空
@if(TempData["ErrorMessage"] != null) { <div class='alert alert-success alert-dismissable' > @TempData["ErrorMessage"] </div> }One way to do that :
public ActionResult Index(string url) { getCredentials(); if (Authenticate(userName, password)) { return View(); } else { TempData["ErrorMessage"]="Wrong Credential"; return Redirect(url); } } }Then in your view Show the message in a div if it's not empty
@if(TempData["ErrorMessage"] != null) { <div class='alert alert-success alert-dismissable' > @TempData["ErrorMessage"] </div> }更多推荐
发布评论