【发布时间】:2014-04-02 17:38:20
【问题描述】:
我正在使用 MVC Razor,但遇到验证错误的问题。这是在一个登录页面中,目前使用此代码:
@using (Html.BeginForm("Login", "Home"))
{
<div id="Login">
@Html.TextBoxFor(model => model.Email, new { type = "Text", id = "url", placeholder = "E-mail Address or User Name", onclick = "this.value = ''" })
@Html.ValidationMessageFor(model=>model.Email)
@Html.TextBoxFor(model => model.Password, new { type = "Password", id = "url", placeholder = "Password", onclick = "this.value = ''" })
@Html.ValidationMessageFor(model=>model.Password)
<div id="submit">
<input type="image" src="~/Content/images/submit_hover.png" id="submit1" value="Sign In">
<input type="image" src="~/Content/images/submit.png" id="submit2" value="Sign In">
</div>
</div>
}
型号代码为:
[Required]
[DataType(DataType.EmailAddress)]
[StringLength(150)]
[Display(Name = "E-mail Address or User Name")]
public string Email { get; set; }
//////
[Required]
[DataType(DataType.Password)]
[StringLength(30, MinimumLength=6)]
[Display(Name = "Password")]
public string Password { get; set; }
控制器的代码:
public ActionResult Login(Models.BULKSMSModel User)
{
if (ModelState.IsValid)
{
if (IsValid(User.Email, User.Password))
{
FormsAuthentication.SetAuthCookie(User.Email, false);
return RedirectToAction("Dashboard","Home");
}
else
{
ModelState.AddModelError("", "Login Data Is incorrect.");
}
}
return View(User);
}
private bool IsValid(string Email, string Password)
{
Encryption Encryption = new Encryption("b2w5i6g4");
var Crypto = Encryption.Encrypt(Password);
bool IsValid = false;
using (var db = new ModelDbEntities())
{
var User = db.USERS.FirstOrDefault(U => U.EMAILID == Email );
if (User != null)
{
if (User.PASSWORD == Encryption.Encrypt(Password))
{
IsValid = true;
}
}
}
return IsValid;
}
如果用户名在数据库中不可用,并且登录密码或用户名无效,我想返回错误。我还希望将文本框边框设为红色。请帮助我,让我知道我的代码有什么问题,并帮助我纠正它。
【问题讨论】:
标签: c# asp.net-mvc validation razor login