i created a login page and a view model for my login page
when i run created page this line will thrown an exception :
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
this is complete login pages code:
@model DataLayer.LoginViewModel
<div class="form-horizontal">
<h4>LoginViewModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Rememberme, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Rememberme)
@Html.ValidationMessageFor(model => model.Rememberme, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
and this is my viewmodel :
public class LoginViewModel
{
[Required]
[MaxLength(150)]
public string UserName { get; set; }
[Required]
[MaxLength(150)]
public string Password { get; set; }
public bool Rememberme { get; set; }
}
this is controller :
ILoginRepository loginRepository;
MyCmsContext db = new MyCmsContext();
public AccountController()
{
loginRepository = new LoginRepository(db);
}
// GET: Account
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(LoginViewModel login)
{
if (ModelState.IsValid)
{
if (loginRepository.IsExistUser(login.UserName,login.Password))
{
FormsAuthentication.SetAuthCookie(login.UserName, login.Rememberme);
return Redirect("/");
}
else
{
ModelState.AddModelError("UserName", "User not found");
}
}
return View();
}
}