This article shows how to Insert Record in ASP.NET MVC Application using Entity Framework. Follow These below steps:
Step-1: Create Table
CREATE TABLE [dbo].[tbl_registration] ( [Loginid] INT IDENTITY (1, 1) NOT NULL, [FullName] VARCHAR (MAX) NULL, [UserName] VARCHAR (MAX) NULL, [Password] VARCHAR (MAX) NULL, [Email] VARCHAR (MAX) NULL, [Age] INT NULL, [Gender] VARCHAR (50) NULL, [Religion] VARCHAR (50) NULL, [Image] VARCHAR (MAX) NULL, PRIMARY KEY CLUSTERED ([Loginid] ASC) );
Step-2: Write below coding in Controller
public ActionResult Registration()
{
return View();
}
[HttpPost]
public ActionResult Submitdetails(string FullName, string UserName,string Password,string Email,int Age,string Gender,string Religion)
{
try
{
tbl_registration tb = new tbl_registration();
tb.FullName = FullName;
tb.UserName = UserName;
tb.Password = Password;
tb.Religion = Religion;
tb.Gender = Gender;
tb.Email = Email;
tb.Age = Age;
ob.tbl_registration.Add(tb);
ob.SaveChanges();
return Json(new { success = true, responseText = "Success" }, JsonRequestBehavior.AllowGet);
}
catch
{
return Json(new { success = false, responseText = "Error" }, JsonRequestBehavior.AllowGet);
}
}
Step-3:Add View "Registration". And write these below coding in the view page
@*@{
Layout = null;
}*@
<div style="height:100px;"></div>
<div>
<table width="60%" align="center" style="border:1px solid;">
<tr>
<td colspan="2" style="border-bottom:2px solid; background-color:rgba(216, 202, 202, 0.59)"><h3>New User Registraton : </h3></td>
</tr>
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td width="50%" align="right">
<h5>FULL NAME : </h5>
</td>
<td width="50%" align="left">
<input id="txtfullname" type="text" placeholder="FULL NAME" required/>
</td>
</tr>
<tr>
<td width="50%" align="right">
<h5>USER NAME : </h5>
</td>
<td width="50%" align="left">
<input id="txtusername1" type="text" placeholder="USER NAME" required/>
</td>
</tr>
<tr>
<td width="50%" align="right">
<h5>Password : </h5>
</td>
<td width="50%" align="left">
<input id="txtpassword" type="password" placeholder="PASSWORD" required/>
</td>
</tr>
<tr>
<td width="50%" align="right">
<h5>Re-Enter Password : </h5>
</td>
<td width="50%" align="left">
<input id="txtrepassword" type="password" placeholder="RE-ENTER PASSWORD" required/><label id="lblChk" style="color:red;"></label>
</td>
</tr>
<tr>
<td width="50%" align="right">
<h5>E-mail : </h5>
</td>
<td width="50%" align="left">
<input id="txtemail" type="email" placeholder="E-MAIL" required />
</td>
</tr>
<tr>
<td width="50%" align="right">
<h5>Age : </h5>
</td>
<td width="50%" align="left">
<input id="txtage" type="number" placeholder="AGE" required />
</td>
</tr>
<tr>
<td width="50%" align="right">
<h5>Gender : </h5>
</td>
<td width="50%" align="left">
<input id="rbmale" type="radio" value="Male" name="gender" />Male
<input id="rbfemale" type="radio" value="Female" name="gender" />Female
</td>
</tr>
<tr>
<td width="50%" align="right">
<h5>Religion : </h5>
</td>
<td width="50%" align="left">
<select id="ddlreligion">
<option value="Select">Select</option>
<option value="Hindu">Hindu</option>
<option value="Muslim">Muslim</option>
<option value="Sikh">Sikh</option>
<option value="Christian">Christian</option>
</select>
</td>
</tr>
<tr>
<td width="50%" align="right"></td>
<td width="50%" align="left"></td>
</tr>
<tr>
<td width="50%" align="right"></td>
<td width="50%" align="left"></td>
</tr>
<tr>
<td width="50%" align="right">
<input id="btncancel" type="button" value="CANCEL" />
</td>
<td width="50%" align="left">
<input id="btnlogin" type="button" value="Submit" />
</td>
</tr>
<tr>
<td width="50%" align="right"></td>
<td width="50%" align="left"></td>
</tr>
</table>
</div>
<script>
$(document).ready(function () {
//Clear all details
$('#btncancel').click(function () {
debugger;
$('#txtfullname').val("");
$('#txtusername1').val("");
$('#txtpassword').val("");
$('#txtrepassword').val("");
$('#txtemail').val("");
$('#txtage').val("");
$("#rbmale").attr('checked', false);
$("#rbfemale").attr('checked', false);
$("#ddlreligion").val("Select");
});
//Check password
$('#txtrepassword').change(function () {
debugger;
if ($('#txtrepassword').val() == $('#txtpassword').val())
$("#lblChk").text("");
else
$("#lblChk").text("Passwords are not same");
});
//Save all details
$('#btnlogin').click(function () {
debugger;
if ($('#txtrepassword').val() == $('#txtpassword').val()) {
var fullname = $('#txtfullname').val();
var username = $('#txtusername1').val();
var password = $('#txtpassword').val();
var mail = $('#txtemail').val();
var age = $('#txtage').val();
var gender;
if (document.getElementById('rbmale').checked)
gender = $("#rbmale").val();
else
gender = $("#rbfemale").val();
var religion = $("#ddlreligion").val();
$.ajax({
contentType: "application/json; charset=utf-8",
url: "/Authentication/Submitdetails",
type: "POST",
dataType: "json",
data: '{"FullName":"'+fullname+'","UserName": "'+username+'","Password": "'+password+'","Email": "'+mail+'","Age": "'+age+'","Gender": "'+gender+'","Religion": "'+religion+'"}',
success: function (response) {
if (response != null && response.success) {
window.location = "/Authentication/Index";
} else {
// DoSomethingElse()
window.location = "/Authentication/Registration";
}
},
error: function (response) {
alert("error!"); //
}
});
}
else
$("#lblChk").text("Passwords are not same");
});
});
</script>
0 Comments