In this article I will show you how you can create a login
form you mvc application by validating user from data base. In this I have used
MVC3, C#.Net and EntityFrameWork .This code is compatible with MVC2/MVC3/MVC4/MVC5.
Some of my previous articles are as follows:
Simple
Login Form In Asp.Net Using C#.Net and VB.Net, Login
Form With LightbBox Effect in Asp.Net, Show
and Hide Div on Button Click Using jQuery in Asp.Net | How to Use jQuery Toggle
Function on Click In Asp.net, How
to Use and Install Ajaxcontroltoolkit in Asp.Net, Bind
and Retrieve ListBox Selected Value in MVC Using C#.Net, Integrate
and Retrive HTML Textbox Editor Value In Asp.Net MVC Using C#.Net, File
Upload and Display Uploaded Previsw in MVC3 Razor Example Using C#.Net.
So for this article first we will create a new mvc
application. After this we will create a UserDetail table in sql DB. This table we will use in out entity file.
Now we will add mode class as shown below.
Field Name
|
Data Type
|
Id
|
Int
|
UserId
|
Varchar(50)
|
Password
|
Varchar(50)
|
Now we will add mode class as shown below.
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCDemo.Models
{
public class UserLoginModel
{
public
string UserId { get;
set; }
public
string Password { get;
set; }
}
}
|
After creating model we will create a controller class in
controller folder, and add the below code.
Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Models;
namespace MVCDemo.Controllers
{
public class UserLoginController
: Controller
{
//
// GET:
/UserLogin/
public
ActionResult Index()
{
UserLoginModel
_objuserloginmodel = new UserLoginModel();
return
View(_objuserloginmodel);
}
[HttpPost]
public
ActionResult Index(UserLoginModel _objuserloginmodel)
{
/*Create
instance of entity model*/
NorthwindEntities
objentity = new NorthwindEntities();
/*Getting
data from database for user validation*/
var
_objuserdetail = (from data in objentity.UserDetails
where data.UserId == _objuserloginmodel.UserId
&& data.Password ==
_objuserloginmodel.Password
select data);
if
(_objuserdetail.Count() > 0)
{
/*Redirect
user to success apge after successfull login*/
ViewBag.Message = 1;
}
else
{
ViewBag.Message = 0;
}
return
View(objuserloginmodel);
}
}
}
|
In above code we have defined the httpget and httppost
method in this httpget method is used for firing page load events and httppost
when the we click on submit button to raise the button click event. In this
method we will validating the user from data base by using linq query.
After completing controller code we will create view for the
controller action.
View
@model MVCDemo.Models.UserLoginModel
@{
ViewBag.Title = "Simple
Login From in Asp.Net MVC Using C#.Net";
}
<h2>
Simple Login From in MVC</h2>
@using
(@Html.BeginForm("Index", "UserLogin"))
{
<table width="25%"
style="border: 1 solid back; border-collapse:
collapse;" border="1" cellpadding="5" cellspacing="5">
<tr>
<td colspan="2" style="background-color: Gray;">LOGIN
</td>
</tr>
<tr>
<td align="right"> User Id:</td>
<td align="left">@Html.TextBoxFor(m => m.UserId, new { @id = "txtuserid",@style="width:200px;" })</td>
</tr>
<tr>
<td align="right">Password:</td>
<td align="left">@Html.PasswordFor(m => m.Password, new { @id = "txtpasword",
@style = "width:200px;" }) </td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="Login" />
</td>
</tr>
</table>
if
(ViewBag.Message == 1)
{
<script language="javascript">
alert("Successfull
login.");
</script>
}
if
(ViewBag.Message == 0)
{
<script language="javascript">
alert("Please
provide correct userid or password.");
</script>
}
}
|
Now run the application
periya tom cruse ne velaya paru da
ReplyDelete