Asp.net mvc 登录未通过从视图到控制器的LoginStudentNumber
当我运行登录时,我似乎无法通过LoginStudentNumber。它读取密码,但不读取LoginStudentNumber。请看看我的代码到底出了什么问题 登录控制器:Asp.net mvc 登录未通过从视图到控制器的LoginStudentNumber,asp.net-mvc,login,Asp.net Mvc,Login,当我运行登录时,我似乎无法通过LoginStudentNumber。它读取密码,但不读取LoginStudentNumber。请看看我的代码到底出了什么问题 登录控制器: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using RMS.DAL; using RMS.Models; namespace RMS.Controlle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMS.DAL;
using RMS.Models;
namespace RMS.Controllers
{
public class LoginController : Controller
{
RMSContext db = new RMSContext();
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
public ActionResult Login(Login model, int? LoginStudentNumber, string LoginPassword)
{
//Login user = db.Logins
Login user = db.Logins.Find(LoginStudentNumber);
if (user == null)
{
ViewBag.Name1 = "Invalid User ID";
return View();
}
else
{
if (user.LoginPassword.Equals(LoginPassword))
{
return View("../Home/Index", model);
}
else
{
ViewBag.Name2 = "Invalid Password";
return View();
}
}
}
}
}
登录视图:
@model RMS.Models.Login
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Creative - Bootstrap 3 Responsive Admin Template">
<meta name="author" content="GeeksLabs">
<meta name="keyword" content="Creative, Dashboard, Admin, Template, Theme, Bootstrap, Responsive, Retina, Minimal">
<link rel="shortcut icon" href="img/favicon.png">
<title>Login</title>
<!-- Bootstrap CSS -->
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<!-- bootstrap theme -->
<link href="~/Content/bootstrap-theme.css" rel="stylesheet" />
<!--external css-->
<!-- font icon -->
<link href="~/Content/elegant-icons-style.css" rel="stylesheet" />
<link href="~/Content/font-awesome.css" rel="stylesheet" />
<!-- Custom styles -->
<link href="~/Content/style.css" rel="stylesheet" />
<link href="~/Content/style-responsive.css" rel="stylesheet" />
<!-- HTML5 shim and Respond.js IE8 support of HTML5 -->
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->
</head>
<body style="background-color:white">
<img src="~/img/thrupps.png" style="width: 300px; height: 200px; margin: 0px 490px 0px 490px; " />
<div class="col-md-12">
<div class="login-wrap">
@using (Html.BeginForm("Login", "Login", System.Web.Mvc.FormMethod.Post, new { @class = "login-form" }))
{
<p class="login-img"><i class="icon_lock_alt"></i></p>
<div>
@ViewBag.Name1
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icon_profile"></i></span>
@Html.TextBoxFor(a => a.LoginStudentNumber, "LoginStudentNumber", htmlAttributes: new { @class = "form-control" })
</div>
<div>
@ViewBag.Name2
</div>
<div class="input-group">
<span class="input-group-addon"><i class="icon_key_alt"></i></span>
@Html.TextBoxFor(a => a.LoginPassword, "LoginPassword", htmlAttributes: new { @class = "form-control" })
</div>
<input type="submit" id="button" class="btn btn-lg btn-success btn-block " value="Login" />
}
</div>
</div>
</body>
</html>
还要补充的是,当我输入以下代码时,我识别“Login”而不是“Login”,即使我的表标题为“Login”
虽然迈克尔·冯的回答确实让我发痒,但我相信这个答案正是你想要的 逻辑操作的参数拼写错误。您将
LoginStudentNumer
设置为LoginStudentNumer
public ActionResult Login(Login model, int? LoginStudentNumer, string LoginPassword)
更新
通过使用type属性并将其值设置为number,可以确保客户端输入一个数字
@Html.TextBoxFor(a => a.LoginStudentNumber, "LoginStudentNumber", htmlAttributes: new { @class = "form-control", type = "number" })
更新2
还是有问题。当我运行登录页面时,它不会占用我的时间 当我的用户名和密码正确时,请转到主页 它不带您访问主页的原因是因为以下内容无效
return View("../Home/Index", model);
您应该返回指向浏览器的重定向
return RedirectToAction("Index", "Home");
当我的用户名和密码为
不对。控制器可能有问题
如果要显示错误,可以返回模型以及任何模型状态错误
然后在您的视图中,您需要输出错误消息,但使用
@Html.ValidationSummary()
这不是一个合适的评论,谢谢。我确实发现了那个错误,但是我面临着同样的问题。(对不起,我意识到我应该更新我的问题)你确定吗?我已经复制了你的代码和它的工作为我找到。确保您实际键入的是一个整数,否则它仍然会有问题。当我运行登录页面时,当我的用户名和密码正确时,它不会将我带到主页,当我的用户名和密码不正确时,它也不会显示错误。控制器可能有问题。好的,我添加了一个,但更多!如果你还需要什么,你最好提出一个新问题,因为这是一个分歧
return RedirectToAction("Index", "Home");
public ActionResult Login(Login model, int? LoginStudentNumber, string LoginPassword)
{
//Login user = db.Logins
Login user = db.Logins.Find(LoginStudentNumer);
if (user == null)
{
ModelState.AddModelError("", "Invalid User Id");
return View(model);
}
if (user.LoginPassword.Equals(LoginPassword))
{
return RedirectToAction("Index","Home");
}
ModelState.AddModelError("", "Invalid Password");
return View(model);
}
@Html.ValidationSummary()