Hello Team, please in my mvc application am working with parent and child table, tblStaff and tblcompany respectively and this two table has been joined and when I try to update data into the database through view model, the data keep dupplicating, kindly assist.
Loading
Vishal JoshiPosted Jun 2, 2023, 4:07 AM
Hello,
It looks need to fetch parent and child table details first if it's in edit mode otherwise it will always duplicate. please try the below code to get it to work.
Thanks
Vishal
Emmmanuel FIADUFEPosted Jun 2, 2023, 3:38 PM
Wwwwwww thank you Vishal, it working but not stable, I think I need to share my html code as well for you to see
Emmmanuel FIADUFEPosted Jun 1, 2023, 8:29 PM
Thank you so much Swain for the guidelines, I check and revert
Rajkiran SwainPosted Jun 1, 2023, 3:57 PM
To address the issue of data duplication when updating parent-child tables (tblStaff and tblCompany) in your MVC application, you need to ensure that you handle the update process correctly. Here are a few steps to help you troubleshoot and resolve the problem:
1. Verify the data flow:
- Check if the data is being duplicated during the update process.
- Ensure that the update action is triggered only once.
2. Review the code in your update action method:
- Examine the code responsible for updating the data in the parent and child tables.
- Check if there are any loops or conditions that might cause duplicate updates.
- Confirm that the update process is properly structured and follows the intended logic.
3. Check the relationships between the parent and child tables:
- Verify that the relationship between tblStaff and tblCompany is correctly defined in the database.
- Ensure that the foreign key relationship is properly established.
4. Inspect the view model and data binding:
- Confirm that the view model used for updating data accurately represents the structure of the parent and child tables.
- Check if the data binding in the update view is correctly mapped to the view model.
- Ensure that the data passed from the view to the controller action is accurate and not duplicated.
5. Validate the update process:
- Implement server-side validation to prevent duplicate data entry.
- Check if the update process is properly checking for existing records and avoiding duplicates.
6. Debug and trace the application:
- Utilize debugging techniques to step through the code and identify any areas where data duplication might occur.
- Use logging or debugging statements to track the flow of data and identify any unexpected behavior.
7. Test with sample data:
- Create a test scenario with sample data and execute the update process.
- Observe the behavior of the application and analyze the results.
- This will help isolate the specific conditions or inputs that lead to data duplication.
By thoroughly reviewing your code, data flow, relationships, and validation processes, you should be able to identify and address the issue of data duplication during the update process in your MVC application.
Emmmanuel FIADUFEPosted Jun 1, 2023, 1:29 PM
Controller Code
[HttpPost]
public ActionResult SaveStaff(StaffManagementViewModel model)
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
var result = false;
try
{
tblCompany comp = new tblCompany();
comp.DateJoin = model.tblCompany.DateJoin;
comp.Department = model.tblCompany.Department;
comp.JobTitle = model.tblCompany.JobTitle;
if (model.tblCompany.CompanyId <= 0)
db.tblCompanies.Add(comp);
db.SaveChanges();
model.CompanyId = comp.CompanyId;
tblStaff staff = new tblStaff();
staff.StaffNo = model.StaffNo;
staff.FName = model.FName;
staff.LName = model.LName;
staff.BirthDate = DateTime.Now.ToString("dd-MM-yyyy");
staff.PhoneNo = model.PhoneNo;
staff.Email = model.Email;
staff.FirstName = model.FirstName;
staff.LastName = model.LastName;
staff.PhoneNumb = model.PhoneNumb;
staff.CompanyId = model.CompanyId;
db.SaveChanges();
result = true;
}
catch (Exception ex)
{
throw ex;
}
return Json(result, JsonRequestBehavior.AllowGet);
}