public APIModulesController(ApplicationDbContext context)
 {
     _context       = context;
     FieldViewModel = new FeildModelView();
     FieldViewModel.ModuleFieldDetails     = new ModuleFieldDetails();
     FieldViewModel.ModuleFieldDetailsList = new List <ModuleFieldDetails>();
     FieldViewModel.FieldType = new SelectList(_context.FieldTypes.ToList(), "ID", "Name");
 }
        public async Task <IActionResult> SaveFieldmodule(FeildModelView model)
        {
            string message = string.Empty;

            //var PsModuleId = new Guid(model.ModuleId);
            try
            {
                bool checkcode = await IsTenantCodeAvailable(model.TenantCode);

                if (checkcode)
                {
                    if (model.ModuleFieldDetails != null)
                    {
                        var ModuleFieldDetails = await _context.ModuleFieldDetails.SingleOrDefaultAsync(d => d.ID == model.ModuleFieldDetails.ID);// && d.ModeuleDetailId== PsModuleId);

                        if (ModuleFieldDetails != null)
                        {
                            //Does not exist    //Update role
                            ModuleFieldDetails.DBFieldName = model.ModuleFieldDetails.DBFieldName;
                            ModuleFieldDetails.FieldLabel  = model.ModuleFieldDetails.FieldLabel;
                            ModuleFieldDetails.FieldType   = model.ModuleFieldDetails.FieldType;
                            ModuleFieldDetails.TenantCode  = model.TenantCode;
                            _context.ModuleFieldDetails.Update(ModuleFieldDetails);
                            await _context.SaveChangesAsync();

                            var ModuleFieldOptions = _context.ModuleFieldOptions.Where(d => d.ModuleFieldDetailsID == model.ModuleFieldDetails.ID).ToList();
                            if (ModuleFieldOptions.Count > 0)
                            {
                                _context.ModuleFieldOptions.RemoveRange(ModuleFieldOptions);
                                await _context.SaveChangesAsync();
                            }
                            if (model.ModuleFieldOption != null)
                            {
                                model.ModuleFieldOption.ForEach(z => z.ModuleFieldDetailsID = model.ModuleFieldDetails.ID);
                                _context.ModuleFieldOptions.AddRange(model.ModuleFieldOption);
                                await _context.SaveChangesAsync();
                            }

                            message = AccountOptions.API_Response_Saved;
                        }
                        else
                        {
                            //Add
                            //checkk if exist
                            if (_context.ModuleFieldDetails.Any(x => x.DBFieldName == model.ModuleFieldDetails.DBFieldName))
                            {
                                message = "DBFieldName Already" + AccountOptions.API_Response_Exist;
                            }
                            else if (_context.ModuleFieldDetails.Any(x => x.FieldLabel == model.ModuleFieldDetails.FieldLabel))
                            {
                                message = "Field Label Already" + AccountOptions.API_Response_Exist;
                            }
                            else
                            {
                                //save [ModuleField]
                                //model.ModuleFieldDetails.ModuleDetailsID= PsModuleId;
                                model.ModuleFieldDetails.TenantCode = model.TenantCode;
                                _context.ModuleFieldDetails.Add(model.ModuleFieldDetails);
                                await _context.SaveChangesAsync();

                                //save [ModuleFieldOptions]
                                if (model.ModuleFieldOption != null)
                                {
                                    model.ModuleFieldOption.ForEach(z => z.ModuleFieldDetailsID = model.ModuleFieldDetails.ID);
                                    _context.ModuleFieldOptions.AddRange(model.ModuleFieldOption);
                                    await _context.SaveChangesAsync();
                                }

                                message = AccountOptions.API_Response_Saved;
                            }
                        }
                    }
                }
                else
                {
                    message = AccountOptions.InvalidTenantErrorMessage;
                }
            }
            catch (Exception ex)
            {
                message = AccountOptions.API_Response_Exception;
            }
            return(Ok(new
            {
                Status = message
            }));
        }