简体   繁体   中英

Razor MVC validation error fix with c# in vs2012

I'm using MVC Razor, and have a problem with getting the validation error. 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.");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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