0

标题说明了一切,任何人都可以发现我做错了什么。我试过移动我的 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();
    }  
4

3 回答 3

5

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

public ActionResult Login(string FirstName, string Password)

将动作的参数更改为:

public ActionResult Login(Login model)

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

  • 如果您包含 jquery 验证插件 (js)
  • 检查您的 web.config、keysClientValidationEnabledUnobtrusiveJavaScriptEnabledir 是否为真。

检查这个链接

于 2013-09-10T02:20:00.927 回答
0

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

于 2013-09-10T14:01:52.690 回答
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();
}

在视图中只有

@using (Html.BeginForm())

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

试试看。

于 2013-09-09T20:55:32.073 回答