private void btnSave_Click(object sender, EventArgs e) { var product = CreateProductByForm(); if (_productId == 0) { using (var dbContext = DbHelper.CreateDbContext()) { dbContext.Products.Add(product); dbContext.SaveChanges(); } } else { product.ProductID = _productId; using (var dbContext = DbHelper.CreateDbContext()) { dbContext.Entry(product).State = EntityState.Modified; dbContext.SaveChanges(); } } Dispose(); FormSaved?.Invoke(); }
private static void saveForm(int IdForm) { FormContext db = new FormContext(); Form f = db.Forms.Find(IdForm); Control pf = findControlI(_Page, "PanelForm" + IdForm); String EmailTo = ""; String Subject = ""; string ValueToSubject = ""; FormRender.IsUpdated = Request.QueryString["IsUpdated"] != null?Convert.ToBoolean(Request.QueryString["IsUpdated"]) : false; List <FormResult> frs = new List <FormResult>(); FormResult fr1 = new FormResult(); if (IsUpdated) { fr1 = db.FormResults.FirstOrDefault(row => row.IdPopupLoad == IdPopupLoad && row.Deleted == 0); frs = db.FormResults.Where(row => row.IdFormSaved == fr1.IdFormSaved).ToList(); } FormSaved fs = new FormSaved(); if (pf != null) { if (!IsUpdated) { fs = new FormSaved(); fs.Created = DateTime.Now; db.FormSaveds.Add(fs); db.SaveChanges(); } else { fs = db.FormSaveds.Find(fr1.IdFormSaved); } var ET = new EmailTemplate(f.EmailTemplate == null ? "" : f.EmailTemplate); string result = ""; result += setting("EMAIL_PRETEXT") + Environment.NewLine; result += "ОБРАЩЕНИЕ №: " + fs.Id + Environment.NewLine; ET.AddParam("АОН", (Request.QueryString["AbonentNumber"] != null ? Request.QueryString["AbonentNumber"].ToString() : "")); result += "АОН: " + (Request.QueryString["AbonentNumber"] != null ? Request.QueryString["AbonentNumber"].ToString() : "") + Environment.NewLine; ET.AddParam("Дата и время обращения", DateTime.Now.ToString()); result += "Дата и время обращения: " + DateTime.Now + Environment.NewLine; ET.AddParam("Оператор", Operator); result += "Оператор: " + Operator + Environment.NewLine; if (f.IdTask == 4) { result += getFramePath(); } List <FormField> FormFields = f.FormFields.Where(p => p.Active == 1 && p.Deleted == 0).ToList(); foreach (FormField ff in FormFields) { FormResult fr = new FormResult(); if (IsUpdated) { fr = frs.FirstOrDefault(row => row.IdField == ff.Id); } else { fr = new FormResult() { Deleted = Request.QueryString["debug"] != null ? 1 : 0, IdFormSaved = fs.Id, IdPopupLoad = IdPopupLoad, IdField = ff.Id, IdForm = ff.IdForm, IdTask = IdTask, IdProject = IdProject, Active = 1, Created = DateTime.Now, Updated = DateTime.Now } }; System.Type T = null; if (ff.FormTypeField.SystemTitle != "") { try { T = System.Web.Compilation.BuildManager.GetType("FormField" + ff.FormTypeField.SystemTitle, true); } catch (Exception e) { }; } ; if (T == null) { switch (ff.FormTypeField.Id) { case 2: handlerFieldDropDownList(fr, ff); if (ff.FormEmails.Count(p => p.IdReferenceBookData == fr.IdValue) > 0) { var objEmail = ff.FormEmails.First(p => p.IdReferenceBookData == fr.IdValue); if (objEmail != null) { EmailTo += (EmailTo == "" ? "" : ", ") + objEmail.Email; Subject += (Subject == "" ? "" : ", ") + objEmail.Subject; } ; } ; break; } } else { object Obj = Activator.CreateInstance(T); Control cObj = (Control)Obj.GetType().InvokeMember("handlerResult", System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, Obj, new object[] { ff, fr, _Page }); } result += ff.Title + ": " + fr.Value + (fr.Note != "" ? ", " : "") + fr.Note + Environment.NewLine; ET.AddParam(ff.Title, fr.Value + (fr.Note != "" ? ", " : "") + fr.Note); if (!IsUpdated) { db.FormResults.Add(fr); } if (ff.InSubjectEmail == true) { ValueToSubject += (ValueToSubject != "" ? " -> " : "") + fr.Value + (fr.Note != "" ? ", " : "") + fr.Note; } } db.SaveChanges(); if (f.SendToEmail == 1) { HiddenField hf = (HiddenField)findControlI(_Page, "PanelFormHiddenFieldEmail" + IdForm); string emails = hf.Value; Label Messager = (Label)findControlI(_Page, "MessagerForm" + IdForm); if (EmailTo != "") { emails += (emails != "" ? "," : "") + EmailTo; } try { if (emails != "") { if (f.IsBodyHtml) { result = result.Replace(Environment.NewLine, "<br/>"); } if (f.EmailTemplate != null && f.EmailTemplate != "" && f.IsBodyHtml) { result = ET.GetFullBody(); } EmailSender.sendEmail(emails, result, (Subject != "" ? Subject : f.Title) + (ValueToSubject != "" ? (" : " + ValueToSubject) : ""), IdTask, IdProject, IdPopupLoad, true, f.Id, f.IsBodyHtml); Messager.Text = "Форма отправлена"; Messager.Visible = true; Messager.ForeColor = System.Drawing.Color.Green; } } catch (Exception err) { Messager.Text = err.Message.ToString(); Messager.Visible = true; Messager.ForeColor = System.Drawing.Color.Red; } } ; if (f.HasSaveButton == 1) { if (f.SendToEmail == 0) { Label Messager = (Label)findControlI(_Page, "MessagerForm" + IdForm); Messager.Text = "Форма сохранена"; Messager.Visible = true; Messager.ForeColor = System.Drawing.Color.Green; } if (!IsUpdated) { foreach (FormField ff in FormFields) { switch (ff.FormTypeField.Id) { case 1: ((TextBox)findControlI(_Page, "FormField" + ff.IdForm + "_" + ff.Id)).Text = ""; break; case 2: DropDownList ddl = (DropDownList)findControlI(_Page, "FormField" + ff.IdForm + "_" + ff.Id); if (ddl.Enabled) { ddl.SelectedIndex = -1; } TextBox tb = (TextBox)findControlI(_Page, "TextBoxForm" + ff.IdForm + "_" + ff.Id); if (tb != null) { tb.Text = ""; } break; } } } } } }