I'm trying to create a Strongly typed login page, when the login page loads and if i try to get some validation on clicking login button it is showing some Error message.
public class LoginDetails
{
[Required]
[EmailAddress]
[Display(Name ="Email Id")]
public string Email { get; set; }
[Required]
[Display(Name ="PassWord")]
public string Password { get; set; }
[Display(Name ="Remember Me?")]
public bool RememberMe { get; set; }
}
public ActionResult Login(LoginDetails model)
{
if (ModelState.IsValid)
{
return RedirectToAction("Index", "Home");
}
return View(model); //Error Occurs Here
}
Showing Error Like Below
Server Error in '/' Application. The view 'Login' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Login/Login.aspx ~/Views/Login/Login.ascx ~/Views/Shared/Login.aspx ~/Views/Shared/Login.ascx ~/Views/Login/Login.cshtml ~/Views/Login/Login.vbhtml ~/Views/Shared/Login.cshtml ~/Views/Shared/Login.vbhtml
By default conventions to ASP.NET MVC, if you don't specify the view name while returning from an action method, it considers the view path to be :
~ [Views Directory] / [Directory with same name as the calling Controller] / [View with same name as Action with extension (.aspx, .ascx, .cshtml, .vbhtml)]
If not found, it also searches the view in shared directory also:
~ [Views Directory] / [Shared Directory] / [View with same name as Action with extension (.aspx, .ascx, .cshtml, .vbhtml)]
In your case, the Login View
is not found in any of the above directories, hence the error. Try to provide the full path of the view to fix the issue as:
public ActionResult Login(LoginDetails model)
{
if (ModelState.IsValid)
{
return RedirectToAction("Index", "Home");
}
//Pass the full view path
return View("~/Views/[Directory in which the view is created]/Login.cshtml", model);
}