Monday, 9 September 2013

MVC Required Field not Working

MVC Required Field not Working

Title says it all, can anyone spot what I'm doing wrong. I've tried moving
around my HTMlValidation Summary and a bunch of other things. I feel like
it may have something to with the views I am returning from my Controller
Class.
-- 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>
}

No comments:

Post a Comment