MVC必填字段不起作用

时间:2013-09-09 20:53:42

标签: asp.net-mvc validation required

标题说明了一切,任何人都可以发现我做错了什么。我试过移动我的HTMlValidation Summary和其他一些东西。我觉得它可能与我从Controller类返回的视图有所不同。

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

3 个答案:

答案 0 :(得分:5)

if (ModelState.IsValid)中的MVC验证模型,并且您未在行动中收到模型:

public ActionResult Login(string FirstName, string Password)

将操作的参数更改为:

public ActionResult Login(Login model)

并且,对于客户端中的验证,请检查是否:

  • 如果包含jquery validate plugin(js)
  • 检查您的web.config,密钥ClientValidationEnabledUnobtrusiveJavaScriptEnabled ir是否为真。

check this link

答案 1 :(得分:0)

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

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

在View中有

@using (Html.BeginForm())

以下是有关MVC Razor表单的链接:http://blog.michaelckennedy.net/2012/01/20/building-asp-net-mvc-forms-with-razor/

试试。

答案 2 :(得分:0)

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