Here I am going to Show how to Edit DataBase Data with the use of MVC
I have write 2 Types of Controller here.
//inside the bracket () am writing the Model, Controller, View Names
Model Class(Emp_tb):-
public partial class Emp_tb
{
public int Id { get; set; }
public string Name { get; set; }
public Nullable<int> Age { get; set; }
public Nullable<System.DateTime> DOB { get; set; }
public string Sex { get; set; }
public string Address { get; set; }
public Nullable<bool> Active { get; set; }
}
Controller1(NewEg):-
// First Method
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Emp_tb personalDetail = db.Emp_tb.Find(id);
if (personalDetail == null)
{
return HttpNotFound();
}
return View(personalDetail);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Name,Age,DOB,Sex,Address")] Emp_tb personalDetail)
{
if (ModelState.IsValid)
{
db.Entry(personalDetail).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(personalDetail);
}
Controller2(NewEg):-
//Edit2
public ActionResult Edit2(int id = 0)
{
Emp_tb emp = db.Emp_tb.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
//
// POST: /Movies/Edit/5
[HttpPost]
public ActionResult Edit2(Emp_tb emp1)
{
if (ModelState.IsValid)
{
db.Entry(emp1).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(emp1);
}
View(Edit):-
@model final1.Models.Emp_tb
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Edit all Details</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new
{
@class =
"control-label col-md-2"
})
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new
{ @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new {
@class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Age, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Age, new { htmlAttributes = new
{ @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Age, "", new { @class
= "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DOB, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DOB, new { htmlAttributes = new {
@class = "form-control" } })
@Html.ValidationMessageFor(model => model.DOB, "", new { @class =
"text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Sex, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Sex)
@Html.ValidationMessageFor(model => model.Sex, "", new {
@class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address, "", new {
@class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Edit" class="btn btn-default" />
</div>
</div>
</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
I have write 2 Types of Controller here.
//inside the bracket () am writing the Model, Controller, View Names
Model Class(Emp_tb):-
public partial class Emp_tb
{
public int Id { get; set; }
public string Name { get; set; }
public Nullable<int> Age { get; set; }
public Nullable<System.DateTime> DOB { get; set; }
public string Sex { get; set; }
public string Address { get; set; }
public Nullable<bool> Active { get; set; }
}
Controller1(NewEg):-
// First Method
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Emp_tb personalDetail = db.Emp_tb.Find(id);
if (personalDetail == null)
{
return HttpNotFound();
}
return View(personalDetail);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Name,Age,DOB,Sex,Address")] Emp_tb personalDetail)
{
if (ModelState.IsValid)
{
db.Entry(personalDetail).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(personalDetail);
}
Controller2(NewEg):-
//Edit2
public ActionResult Edit2(int id = 0)
{
Emp_tb emp = db.Emp_tb.Find(id);
if (emp == null)
{
return HttpNotFound();
}
return View(emp);
}
//
// POST: /Movies/Edit/5
[HttpPost]
public ActionResult Edit2(Emp_tb emp1)
{
if (ModelState.IsValid)
{
db.Entry(emp1).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(emp1);
}
View(Edit):-
@model final1.Models.Emp_tb
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Edit all Details</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new
{
@class =
"control-label col-md-2"
})
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new
{ @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new {
@class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Age, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Age, new { htmlAttributes = new
{ @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Age, "", new { @class
= "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DOB, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DOB, new { htmlAttributes = new {
@class = "form-control" } })
@Html.ValidationMessageFor(model => model.DOB, "", new { @class =
"text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Sex, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Sex)
@Html.ValidationMessageFor(model => model.Sex, "", new {
@class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class =
"control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Address)
@Html.ValidationMessageFor(model => model.Address, "", new {
@class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Edit" class="btn btn-default" />
</div>
</div>
</div>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
EmoticonEmoticon