Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Razor MVC验证错误修复vs2012中的c#_C#_Asp.net Mvc_Validation_Razor_Login - Fatal编程技术网

Razor MVC验证错误修复vs2012中的c#

Razor MVC验证错误修复vs2012中的c#,c#,asp.net-mvc,validation,razor,login,C#,Asp.net Mvc,Validation,Razor,Login,我正在使用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.

我正在使用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>
}
控制器的代码:

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

如果数据库中没有用户名,并且登录密码或用户名无效,我想返回一个错误。我还希望文本框边框变为红色。请帮助我,让我知道我的代码有什么问题,并帮助我纠正它

在您的控制器中调用

AddModelError(“,”登录数据不正确。“)

这是将验证错误添加到类级别,因为没有提供属性名称,因此除非您显示验证摘要,否则它不会显示在表单上。如果您希望在这些字段附近显示错误消息,您也可以为其中一个/两个“电子邮件”和“密码”添加名称

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