public static void AddAjaxGet(this Controller controller, string url, string title, Action <AjaxFormModel> config = null) { AjaxFormModel ajaxFormModel = new AjaxFormModel() { Action = url, FormTitle = title, HttpMethod = "POST" }; if (config != null) { config(ajaxFormModel); } controller.ViewData["AjaxFormModel"] = ajaxFormModel; }
public ActionResult ProcessAjaxForm(FormCollection formCollection) { // get form entity by id // <param name="id">The Entity Identifier in format ComponentID-TemplateID.</param> string formId = formCollection["formId"]; if (string.IsNullOrEmpty(formId)) { throw new Exception("FormdId was not found in form values"); } EntityModel entity = this.ContentProvider.GetEntityModel(formId, WebRequestContext.Localization); AjaxFormModel form = entity as AjaxFormModel; //TODO: replace this code, get form from cache foreach (var field in form.FormFields.Where(f => !string.IsNullOrEmpty(f.OptionsCategory))) { if (field.FieldType == FieldType.DropDown || field.FieldType == FieldType.CheckBox || field.FieldType == FieldType.RadioButton) { List <OptionModel> options = TaxonomyProvider.LoadOptionsFromCategory(field.OptionsCategory, WebRequestContext.Localization); field.OptionsCategoryList = options; } } if (form != null) { MapRequestFormData(form); if (ModelState.IsValid) { try { //Add logic to save form data here foreach (var postAction in form.FormPostActions) { string formHandlerName = string.Empty; try { formHandlerName = postAction.FormHandler; var formHandler = FormHandlerRegistry.Get(formHandlerName); if (formHandler != null) { formHandler.ProcessForm(formCollection, form.FormFields, postAction); } } catch (Exception ex) { if (form.ErrorRedirect != null) { Log.Error(ex, "Error occured while processing form data with form handler: {0}. Error message: {1}", formHandlerName, ex.Message); return(PartialView("~/Areas/Forms/Views/Forms/ErrorResult.cshtml", form.ErrorRedirect)); } else { throw ex; } } } if (form.SuccessRedirect != null) { return(PartialView("~/Areas/Forms/Views/Forms/SuccessResult.cshtml", form.SuccessRedirect)); } else { return(Redirect(WebRequestContext.Localization.Path)); } } catch (Exception ex) { if (form.ErrorRedirect != null) { Log.Error(ex, "Error occured while saving form data."); return(PartialView("~/Areas/Forms/Views/Forms/ErrorResult.cshtml", form.ErrorRedirect)); } else { throw ex; } } } } return(View()); }