This article shows how to Update one 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 Profile()
{
var v = (from a in ob.tbl_registration.ToList()
where a.Loginid == int.Parse(Session["Id"].ToString())
select a).FirstOrDefault();
TempData["Name"] = v.FullName;
TempData["email"] = v.Email;
TempData["Age"] = v.Age;
TempData["Gender"] = v.Gender;
TempData["Religion"] = v.Religion;
return View();
}
public ActionResult Updatedetails(string FullName, string Email, int Age, string Gender, string Religion)
{
try
{
int id=int.Parse(Session["Id"].ToString());
Server.MapPath("~/Demo_mvc_application/");
tbl_registration tb = ob.tbl_registration.Where(b=>b.Loginid==id).FirstOrDefault();
tb.FullName = FullName;
tb.Religion = Religion;
tb.Gender = Gender;
tb.Email = Email;
tb.Age = Age;
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 "Profile". And write these below coding in the view page
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<div class="col-md-12">
<div class="clearfix" style="margin-bottom:1%"></div>
<div class="form-group">
<div class="col-md-4">
Full Name :
</div>
<div class="col-md-8">
<input id="txtname" type="text" value=@TempData["Name"] />
</div>
</div>
<div class="clearfix" style="margin-bottom:1%"></div>
<div class="form-group">
<div class="col-md-4">
Email :
</div>
<div class="col-md-8">
<input id="txtemail" type="text" value=@TempData["email"] />
</div>
</div>
<div class="clearfix" style="margin-bottom:1%"></div>
<div class="form-group">
<div class="col-md-4">
Age :
</div>
<div class="col-md-8">
<input id="txtage" type="text" value=@TempData["Age"] />
</div>
</div>
<div class="clearfix" style="margin-bottom:1%"></div>
<div class="form-group">
<div class="col-md-4">
Gender :
</div>
<div class="col-md-8">
<input id="rbmale1" type="radio" value="Male" class="radio" name="gender" />Male
<input id="rbfemale1" type="radio" value="Female" class="radio" name="gender" />Female
</div>
</div>
<div class="form-group">
<div class="col-md-4">
Religion :
</div>
<div class="col-md-8">
<select id="ddlreligion1">
<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>
</div>
</div>
<div class="form-group">
<div class="col-md-4">
Choose Profilepic :
</div>
<div class="col-md-8">
<input type="file" name="ImageData" id="ImageData" />
</div>
</div>
<div class="clearfix" style="margin-bottom:1%"></div>
<div class="form-group" style="text-align:center;">
<input id="btnupdate" type="button" value="Update" />
</div>
<div class="clearfix" style="margin-bottom:1%"></div>
</div>
<script>
$(document).ready(function () {
debugger;
$('#ddlreligion1').val('@TempData["Religion"]').prop('selected', true);
var chk='@TempData["Gender"]';
if ($("#rbmale1").val()==chk)
$("#rbmale1").attr('checked', true)
else
$("#rbfemale1").attr('checked', true)
//Save all details
$('#btnupdate').click(function () {
debugger;
var fullname = $('#txtname').val();
var mail = $('#txtemail').val();
var age = $('#txtage').val();
var gender;
if (document.getElementById('rbmale1').checked)
gender = $("#rbmale1").val();
else
gender = $("#rbfemale1").val();
//var image = $("#ImageData").selected.val();
var religion = $("#ddlreligion1").val();
$.ajax({
contentType: "application/json; charset=utf-8",
url: "/Authentication/Updatedetails",
type: "POST",
dataType: "json",
data: '{"FullName":"' + fullname + '","Email": "' + mail + '","Age": "' + age + '","Gender": "' + gender + '","Religion": "' + religion + '"}',
success: function (response) {
if (response != null && response.success) {
window.location = "/Authentication/Profile";
} else {
// DoSomethingElse()
window.location = "/Authentication/Profile";
}
},
error: function (response) {
alert("error!"); //
}
});
});
});
</script>
0 Comments