【问题标题】:MVC Required Field not WorkingMVC 必填字段不起作用
【发布时间】:2013-09-09 20:53:42
【问题描述】:

标题说明了一切,任何人都可以发现我做错了什么。我已经尝试过移动我的 HTMlValidation Summary 和其他一些东西。我觉得这可能与我从控制器类返回的视图有关。

-- Model
 public class Login
{
    [Required(ErrorMessage = "First Name is required")]
    [Display(Name = "First Namex")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Password is required")]
    [DataType(DataType.Password)]
    [Display(Name = "Passwordx")]
    public string Password { get; set; }
}

  -- Controller
[HttpPost]
    public ActionResult Login(string FirstName, string Password)
    {
        if (ModelState.IsValid)
        {
            bool validLogin = new UserBAL().ValidateUser(FirstName, Password);
            {
                if (validLogin == true)
                {
                    return RedirectToAction("Index", "Invoice");
                }
                else
                {
                  return RedirectToAction("Index");
                  //  ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
        }
        return View();
    }

 --View 
@using (Html.BeginForm("Login", "Home"))  
{ @Html.ValidationSummary(true)
<div>
    <fieldset>   
        <legend>Login</legend>       
        <div class ="fields">              
          @Html.LabelFor(u => u.FirstName)
          </div>
          @Html.TextBoxFor(u => u.FirstName)
          @Html.ValidationMessageFor(u => u.FirstName)  <br />       

        <div class ="fields">            
         @Html.LabelFor(u => u.Password)
        </div>
         @Html.PasswordFor(u => u.Password) <br />                 
        <input type="submit" value="Log In" />
    </fieldset>
</div>   

}


[HttpPost]
    public ActionResult Login(EIAS.Models.Login login)
    {
        if (ModelState.IsValid)
        {
            bool validLogin = new UserBAL().ValidateUser(login);
            {
            if (validLogin == true)
                {
                    return RedirectToAction("Index", "Invoice");
                }
                else
                {
                  return RedirectToAction ("Index");
                  //  ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
        }
        return View();
    }  

【问题讨论】:

    标签: asp.net-mvc validation required


    【解决方案1】:

    if (ModelState.IsValid) 中的 MVC 验证模型,但您的操作中没有收到模型:

    public ActionResult Login(string FirstName, string Password)
    

    将动作的参数更改为:

    public ActionResult Login(Login model)
    

    并且,为了在客户端进行验证,检查是否:

    • 如果包含 jquery 验证插件 (js)
    • 检查您的 web.config,键 ClientValidationEnabledUnobtrusiveJavaScriptEnabled ir 为 true。

    check this link

    【讨论】:

      【解决方案2】:

      您必须将模型作为登录操作的参数

      public ActionResult Login()
      {
          // This will return the view with the form that you have above
          return View();
      }
      
      [HttpPost]
      public ActionResult Login(Login login)
      {
          // this is what your form will post too
          if (ModelState.IsValid)
          {
              bool validLogin = new UserBAL().ValidateUser(login.FirstName, login.Password);
              {
                  if (validLogin == true)
                  {
                      return RedirectToAction("Index", "Invoice");
                  }
                  else
                  {
                    return RedirectToAction("Index");
                    //  ModelState.AddModelError("", "The user name or password provided is incorrect.");
                  }
              }
          }
          return View();
      }
      

      在视图中只有

      @using (Html.BeginForm())
      

      这里是一个关于 MVC Razor Forms 的链接:http://blog.michaelckennedy.net/2012/01/20/building-asp-net-mvc-forms-with-razor/

      试试看。

      【讨论】:

      • 好的,那么我如何将文本框值传递给我的业务层函数,因为它需要 2 个参数,名字和密码
      • 是的,我将其设置为 ValidateUser(login) 并通过类似的层传递,然后才能使用登录。“字段”
      【解决方案3】:

      问题在于我的视图以及我返回的视图。我试图使用我的索引视图进行验证,但我没有创建登录视图,所以一旦我这样做并将我的验证添加到登录视图中它就可以工作了。所以在我的问题中,Return View() 并没有真正返回任何东西

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-10
        相关资源
        最近更新 更多