I ve been looking for a straight forward way to prompt error messages, ideally a popup, (javascript or otherwise) to users failing the login system on this form:
@model Blog.Models.ViewModels.LoginView
<!DOCTYPE html>
<html>
<body>
@using (Html.BeginForm()) {
<p>Your name: @Html.TextBoxFor(x => x.name) </p>
<p>Your password: @Html.TextBoxFor(x => x.password)</p>
<input type="submit" value="Login" />
}
</body>
</html>
but most examples I ve found use the onclick method on a label. Instead i want it to be handled by this controller:
private BlogModel model = new BlogModel();
[HttpPost]
public ActionResult LoginForm(string name, string password)
{
SysUser user = model.SysUsers.Where(x => x.SysUserName == name).First();
{
if (user != null && user.SysPassword == password)
{
Session["usrn"] = name;
return RedirectToAction("LoginSuccessful", "Users");
}
else
{
//display error//
}
}
I would recommend against a popup or alert() dialog. Instead, I would use a styled div to display your error messages. If you’re using jQuery UI, you could try my message plugin. Since its displayed with javascript, you can use it like so:
Controller:
Markup with jquery-message:
Or if you prefer, without jquery-message: