Forms in Edit Mode in MVC5 | Part 37

Опубликовано: 15 Март 2026
на канале: Syed Ali
584
5

Forms in Edit Mode in MVC5:
Requirement: Visual studio 2015, SQL server 2012 or higher version
Here We will implement the Editing in MVC 5 with one text box and one check box list and update the data into database.
Step1: create the table like hobbies_master(Master table) , tbl_employee(transaction table), tbl_employee_hobbie(transaction table).
Step 2:
Add ADO.NET ENITITY framework in the folder model, The Model1.edms file will be added in the model and when you click on model1.tt you will get the partial class related to tables/stored procedure which you have added, I will show you in the example.
Step 3:(3.1)
Add a controller ViewEmpController and a code to display all the records of the employee in the index action method:
and also write the code in below controller name to access the DB
TestSampleEntities db = new TestSampleEntities();
public ActionResult Index()
{
var result = db.tbl_employee.ToList();
return View(result);
}

(3.2)Add the action method for edit and write the code to display the data into edit forms
// GET: tbl_employee/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
tbl_employee tbl_employee = db.tbl_employee.Find(id);
var emp_id = tbl_employee.empid;


if (tbl_employee == null)
{
return HttpNotFound();
}
ViewModelEmp obj = new ViewModelEmp();
obj.empid = tbl_employee.empid;
obj.emp_name = tbl_employee.emp_name;
obj.hobbies_master = BindSelectedHObbies(emp_id);
return View(obj);
}

private List less sign hobbies_master greater sign BindSelectedHObbies(int emp_id)
{
List less sign hobbies_master greater sign items = new List less sign hobbies_master greater sign ();
var result = db.sp_get_selected_hobbies(emp_id).ToList();
foreach (var itemValue in result)
{
hobbies_master hb_obj = new hobbies_master();
hb_obj.id = itemValue.id;
hb_obj.hobby_name = itemValue.hobby_name;
hb_obj.isChecked = Convert.ToBoolean(itemValue.isChecked);

items.Add(hb_obj);
}
return items.ToList();
}
(3.3)
Here we write the action method for edit to save the data into tables
// POST: tbl_employee/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult index(ViewModelEmp obj_viewModel)
{
if (ModelState.IsValid)
{ // Update data in Employee Table
tbl_employee obj_emp = db.tbl_employee.Find(obj_viewModel.empid);
obj_emp.empid = obj_viewModel.empid;
obj_emp.emp_name = obj_viewModel.emp_name;
db.Entry(obj_emp).State = EntityState.Modified;
db.SaveChanges();
// deleting the record from employee hobbies
var empid = obj_viewModel.empid;
// check whether it is record found or not
var obj_emp_hobbies = db.tbl_employee_hobbie .Where(x = greater sign x.empid == empid).FirstOrDefault();
if(obj_emp_hobbies!=null)
{ //remove the record from the databasedb.tbl_employee_hobbie.RemoveRange(db.tbl_employee_hobbie.Where(x= greater sign x.empid==empid));
// db.ToDoes.Remove(todo);
// db.Entry(obj_emp_hobbies).State = EntityState.Deleted;
db.SaveChanges();
} // Adding the employee hobbies
var selectedHobbies = obj_viewModel.hobbies_master.Where(x = greater sign x.isChecked == true).ToListless signhobbies_master greater sign ();
foreach(var Hob in selectedHobbies)
{
tbl_employee_hobbie Add_obj_emp_hob = new tbl_employee_hobbie();
Add_obj_emp_hob.empid = obj_viewModel.empid;
Add_obj_emp_hob.hobbie_id = Hob.id;
db.tbl_employee_hobbie.Add(Add_obj_emp_hob);
db.SaveChanges();
}
return RedirectToAction("Index"); } return View(); }