Post multiple entities in single request MVC

Post Multiple Entities in single request in Linq:

public ResponseOut AddEditCustomer(CustomerViewModel customerViewModel, List<CustomerBranchViewModel> customerBranchs, List<CustomerProductViewModel> customerProducts,List<CustomerFollowUpViewModel> customerFollowUps)
{
ResponseOut responseOutBranch = new ResponseOut();
ResponseOut responseOutProduct = new ResponseOut();
ResponseOut responseOut = new ResponseOut();
ResponseOut responseOutSL = new ResponseOut();
ResponseOut responseOutFollowUp = new ResponseOut();
using (TransactionScope transactionscope = new TransactionScope())
{
try
{
Customer customer = new Customer
{
CustomerId = customerViewModel.CustomerId,
CustomerCode = customerViewModel.CustomerCode,
CustomerName = customerViewModel.CustomerName,
ContactPersonName = customerViewModel.ContactPersonName,
Designation = customerViewModel.Designation,
Email = customerViewModel.Email,
MobileNo = customerViewModel.MobileNo,
ContactNo = customerViewModel.ContactNo,
Fax = customerViewModel.Fax,
PrimaryAddress = customerViewModel.PrimaryAddress,
City = customerViewModel.City,
StateId = customerViewModel.StateId,
CountryId = customerViewModel.CountryId,
PinCode = customerViewModel.PinCode,
CSTNo = customerViewModel.CSTNo,
TINNo = customerViewModel.TINNo,
PANNo = customerViewModel.PANNo,
GSTNo = customerViewModel.GSTNo,
ExciseNo = customerViewModel.ExciseNo,
EmployeeId = customerViewModel.EmployeeId,
CustomerTypeId = customerViewModel.CustomerTypeId,
CompanyId = customerViewModel.CompanyId,
CreatedBy = customerViewModel.CreatedBy,
CreditLimit=customerViewModel.CreditLimit,
CreditDays=customerViewModel.CreditDays,
Status = customerViewModel.Customer_Status,
AnnualTurnover = customerViewModel.AnnualTurnover,
GST_Exempt = customerViewModel.GST_Exempt
};

int customerId = 0;
responseOut = dbInterface.AddEditCustomer(customer, out customerId);

if (responseOut.status == ActionStatus.Success)
{
if (customerBranchs != null && customerBranchs.Count > 0)
{
foreach (CustomerBranchViewModel customerBranchViewModel in customerBranchs)
{
CustomerBranch customerBranch = new CustomerBranch
{
CustomerId = customerId,
CustomerBranchId = customerBranchViewModel.CustomerBranchId,
BranchName = customerBranchViewModel.BranchName,
ContactPersonName = customerBranchViewModel.ContactPersonName,
Designation = customerBranchViewModel.Designation,
Email = customerBranchViewModel.Email,
MobileNo = customerBranchViewModel.MobileNo,
ContactNo = customerBranchViewModel.ContactNo,
Fax = customerBranchViewModel.Fax,
PrimaryAddress = customerBranchViewModel.PrimaryAddress,
City = customerBranchViewModel.City,
StateId = customerBranchViewModel.StateId,
CountryId = customerBranchViewModel.CountryId,
PinCode = customerBranchViewModel.PinCode,
CSTNo = customerBranchViewModel.CSTNo,
TINNo = customerBranchViewModel.TINNo,
PANNo = customerBranchViewModel.PANNo,
GSTNo = customerBranchViewModel.GSTNo,
AnnualTurnover = customerBranchViewModel.AnnualTurnover,
Status = true
};
responseOutBranch = dbInterface.AddEditCustomerBranch(customerBranch);
}
}
}
if (customerProducts != null && customerProducts.Count > 0)
{
foreach (CustomerProductViewModel customerProductViewModel in customerProducts)
{
CustomerProductMapping customerProduct = new CustomerProductMapping
{
CustomerId = customerId,
MappingId = customerProductViewModel.MappingId,
ProductId = customerProductViewModel.ProductId,
Status = true
};
responseOutProduct = dbInterface.AddEditCustomerProduct(customerProduct);
}
}
if(customerFollowUps!=null && customerFollowUps.Count>0)
{
foreach(CustomerFollowUpViewModel customerFollowUpViewModel in customerFollowUps)
{
CustomerFollowUp customerFollowUp = new CustomerFollowUp {

CustomerFollowUpId= customerFollowUpViewModel.CustomerFollowUpId,
CustomerId= customerId,
FollowUpActivityTypeId = customerFollowUpViewModel.FollowUpActivityTypeId,
FollowUpDueDateTime=Convert.ToDateTime(customerFollowUpViewModel.FollowUpDueDateTime),
FollowUpReminderDateTime= Convert.ToDateTime(customerFollowUpViewModel.FollowUpReminderDateTime),
FollowUpRemarks= customerFollowUpViewModel.FollowUpRemarks,
Priority= Convert.ToByte(customerFollowUpViewModel.Priority),
FollowUpStatusId= customerFollowUpViewModel.FollowUpStatusId,
FollowUpStatusReason= customerFollowUpViewModel.FollowUpStatusReason,
FollowUpByUserId= customerFollowUpViewModel.FollowUpByUserId,
CreatedBy= customerViewModel.CreatedBy,
CreatedDate=Convert.ToDateTime(customerFollowUpViewModel.CreatedDate)

};
responseOutFollowUp = dbInterface.AddEditCustomerFollowUp(customerFollowUp);
}

}

SL sl = new SL
{
SLId = 0,
SLCode = customerViewModel.CustomerCode,
SLHead = customerViewModel.CustomerName,
RefCode = customerViewModel.CustomerCode,
SLTypeId = 2,
CostCenterId = 0,
SubCostCenterId = 0,
CompanyId = customerViewModel.CompanyId,
CreatedBy = customerViewModel.CreatedBy,
Status = true
};

responseOutSL = dbInterface.AddEditCustomerSL(sl, customerViewModel.CustomerId == 0 ? “Add” : “Edit”);
transactionscope.Complete();
}
catch (TransactionException ex1)
{
transactionscope.Dispose();
responseOut.status = ActionStatus.Fail;
responseOut.message = ActionMessage.ApplicationException;
Logger.SaveErrorLog(this.ToString(), MethodBase.GetCurrentMethod().Name, ex1);
throw ex1;
}
catch (Exception ex)
{
transactionscope.Dispose();
responseOut.status = ActionStatus.Fail;
responseOut.message = ActionMessage.ApplicationException;
Logger.SaveErrorLog(this.ToString(), MethodBase.GetCurrentMethod().Name, ex);
throw ex;
}
}
return responseOut;

}

Leave a Reply

Your email address will not be published. Required fields are marked *