Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have employee record having person record which in turn contains address record. I have designed the viewmodel as below to populate the records from three tables.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Altus1.viewmodels
{
public class EmployeeViewModel
{
public Employee employee { get; set; }
public Address address { get; set; }
public Person person { get; set; }

private AltusEntities context = new AltusEntities();

public EmployeeViewModel(Employee employee, Person person, Address address)
{
this.employee = employee;
this.person = person;
this.address = address;
}
public EmployeeViewModel()
{
this.employee = new Employee();
this.person = new Person();
this.address = new Address();
}
}
}

Please guide me as how can I add/update record in these three tables in the create/edit post action.
Posted

Dont see and Update/Edit/delete Model.

1st you should create your update model for same.

or handle this in controller action like.

public ActionResult Action(Employee emp)
{
//check model state here
YourDatacontext context=new YourDatacontext();

Employee empupdate=new Employee();
empupdate.Name=emp.Name;
empupdate.Age=emp.Age;

context.update(empupdate);

// handle exceptions

return View("YourView");
}
 
Share this answer
 
Edit is fine.. here is the code..

public ActionResult Edit(EmployeeViewModel employeeview)
{
ViewData["disablecontrols"] = "false";
if (ModelState.IsValid)
{
db.Entry(employeeview.employee).State = EntityState.Modified;
db.Entry(employeeview.person).State = EntityState.Modified;
db.Entry(employeeview.address).State = EntityState.Modified;
Employee employeeToSave = db.Employees.Find(employeeview.employee.id);
employeeToSave.Person= employeeview.person;
employeeToSave.Person.Address = employeeview.address;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.EMP_WORKDEPT = new SelectList(db.Departments, "ID", "DEPT_NAME", employeeview.employee.EMP_WORKDEPT);
ViewBag.EMP_DESIGNATION_ID = new SelectList(db.Designations, "ID", "DESIGN_NAME", employeeview.employee.EMP_DESIGNATION_ID);
ViewBag.EMP_LEVEL = new SelectList(db.EmployeeLevels, "id", "DESCRIPTION", employeeview.employee.EMP_LEVEL);
ViewBag.PER_SEX = new SelectList(db.Genders, "id", "DESCRIPTION", employeeview.person.PER_SEX);
ViewBag.ADDR_COUNTRY = new SelectList(db.Countries, "ID", "COUNTRY1", employeeview.address.ADDR_COUNTRY);
ViewBag.ADDR_STATE = new SelectList(db.States, "id", "State1", employeeview.address.ADDR_STATE);
return View(employeeview);
}

The question is how to add records in a single SaveChanges call to data context?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900