yogeshc says:
[DisplayName(
"Email Id"
)]
[Required(ErrorMessage =
"Email ID is Required"
)]
[RegularExpression(
"^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$"
, ErrorMessage =
"Invalid Email Id"
)]
public
string
email {
get
;
set
; }
You had defined required for email field. But email field is blank on post action. So you need to ignore the email field from ModelState.
ModelState.Remove("email");
Refer below sample.
Model
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public class user
{
public int id { get; set; }
public string name { get; set; }
[DisplayName("Email Id")]
[Required(ErrorMessage = "Email ID is Required")]
[RegularExpression("^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$", ErrorMessage = "Invalid Email Id")]
public string email { get; set; }
[DisplayName("Username")]
[Required(ErrorMessage = "Username is Required")]
public string username { get; set; }
[DisplayName("Password")]
[Required(ErrorMessage = "Password is Required")]
[DataType(DataType.Password)]
public string password { get; set; }
[DisplayName("Remember Me")]
public bool RememberMe { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(user u)
{
ModelState.Remove("email");
HttpCookie cookie = new HttpCookie("User");
if (ModelState.IsValid)
{
// Rest of code.
}
return View();
}
}
View
@model ModelState_False_MVC.Models.user
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body class="container">
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div id="loginbox">
<form id="loginform" action="#" class="form-vertical">
<div class="control-group normal_text"> <h3>Admin Login</h3></div>
@Html.ValidationSummary(true, "", new { style = "color:red;" })
<div class="control-group">
<div class="controls">
<div class="main_input_box">
<span class="add-on bg_lg"><i class="icon-user"> </i></span>
@Html.EditorFor(model => model.username, new { htmlAttributes = new { @class = "form-control", placeholder = "Username", @Value = ViewBag.username } })
@Html.ValidationMessageFor(model => model.username, "", new { style = "color:red;" })
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div class="main_input_box">
<span class="add-on bg_ly"><i class="icon-lock"></i></span>
@Html.EditorFor(model => model.password, new { htmlAttributes = new { @class = "form-control", placeholder = "Password", @Value = ViewBag.password } })
@Html.ValidationMessageFor(model => model.password, "", new { style = "color:red;" })
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<div data-toggle="buttons-checkbox" class="btn-group">
@Html.CheckBoxFor(model => model.RememberMe)
@Html.LabelFor(model => model.RememberMe)
</div>
</div>
</div>
<div class="form-actions">
<span class="pull-left">
@Html.ActionLink("Lost password ?", "Recover", "Login", new { area = "" }, new { @class = "flip-link btn btn-info" })
</span>
<span class="pull-right"><input type="submit" value="Login" class="btn btn-success" /></span>
</div>
</form>
</div>
}
</body>
</html>