public ActionResult Edit(int id, string category, string type, int eventId = -1)
        {
            if (!Services.Authorizer.Authorize(StandardPermissions.SiteOwner, T("Not authorized to manage rules")))
                return new HttpUnauthorizedResult();

            var ev = _rulesManager.DescribeEvents().SelectMany(x => x.Descriptors).Where(x => x.Category == category && x.Type == type).FirstOrDefault();

            if (ev == null) {
                return HttpNotFound();
            }

            // if there is no form to edit, save the action and go back to the rule
            if (ev.Form == null) {
                if (eventId == -1) {
                    var rule = _rulesServices.GetRule(id);
                    rule.Events.Add(new EventRecord { Category = category, Type = type });
                }

                return RedirectToAction("Edit", "Admin", new { id });
            }

            // build the form, and let external components alter it
            var form = _formManager.Build(ev.Form);

            // generate an anti forgery token
            AddSubmitButton(form);

            // bind form with existing values).
            if (eventId != -1) {
                var rule = _rulesServices.GetRule(id);
                var eventRecord = rule.Events.Where(a => a.Id == eventId).FirstOrDefault();
                if (eventRecord != null) {
                    var parameters = FormParametersHelper.FromString(eventRecord.Parameters);
                    _formManager.Bind(form, new DictionaryValueProvider<string>(parameters, CultureInfo.InvariantCulture));
                }
            }

            var viewModel = new EditEventViewModel { Id = id, Event = ev, Form = form };
            return View(viewModel);
        }
        public ActionResult EditPost(int id, string category, string type, [DefaultValue(-1)] int eventId, FormCollection formCollection)
        {
            var rule = _rulesServices.GetRule(id);

            var eventRecord = rule.Events.Where(a => a.Id == eventId).FirstOrDefault();

            // add new event record if it's a newly created event
            if (eventRecord == null) {
                eventRecord = new EventRecord { Category = category, Type = type };
                rule.Events.Add(eventRecord);
            }

            var e = _rulesManager.DescribeEvents().SelectMany(x => x.Descriptors).Where(x => x.Category == category && x.Type == type).FirstOrDefault();

            // validating form values
            _formManager.Validate(new ValidatingContext { FormName = e.Form, ModelState = ModelState, ValueProvider = ValueProvider });

            if (ModelState.IsValid) {
                var dictionary = formCollection.AllKeys.ToDictionary(key => key, formCollection.Get);

                // save form parameters
                eventRecord.Parameters = FormParametersHelper.ToString(dictionary);

                return RedirectToAction("Edit", "Admin", new { id });
            }

            // model is invalid, display it again
            var form = _formManager.Build(e.Form);
            AddSubmitButton(form);
            _formManager.Bind(form, formCollection);
            var viewModel = new EditEventViewModel { Id = id, Event = e, Form = form };

            return View(viewModel);
        }