简体   繁体   English

vs2012中使用C#的Razor MVC验证错误修复

[英]Razor MVC validation error fix with c# in vs2012

I'm using MVC Razor, and have a problem with getting the validation error. 我正在使用MVC Razor,但遇到验证错误的问题。 This is in a login page, which currently uses this code: 这是在登录页面中,当前使用以下代码:

@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>
}

The Model code is: 型号代码为:

  [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; }

The code for the Controller: 控制器的代码:

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;
    }

I want to return a error if the user name is not available in the db, and also if the login password or user name is not valid. 如果用户名在数据库中不可用,并且登录密码或用户名无效,我想返回一个错误。 I also want the text box border to be made red. 我还希望将文本框边框设为红色。 Please help me out by letting me know what is wrong with my code, and helping me to correct it. 请通过告诉我代码有什么问题并帮助我纠正它来帮助我。

in your controller when you are calling the 在您呼叫控制器时

ModelState.AddModelError("", "Login Data Is incorrect."); ModelState.AddModelError(“”,“登录数据不正确。”);

this is adding the validation error to the class level as no propery name is given and as such it will not display on your form unless you are displaying a Validation summary, you alternativly could add the name for either/both "Email" "Password" if you prefer the error message be displayed near those fields 这是将验证错误添加到类级别,因为没有给出属性名称,因此除非您显示验证摘要,否则它不会显示在您的表单上,您可以为“电子邮件”或“密码”中的任何一个添加名称如果您希望在这些字段附近显示错误消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM