예제 #1
0
        static CommandForm <T> FormHelper <T>(this HtmlHelper htmlHelper, string formAction, string action, string controller, FormMethod method, IDictionary <string, object> htmlAttributes)
            where T : ICommand, new()
        {
            htmlAttributes = htmlAttributes ?? new Dictionary <string, object>();

            var builder = new TagBuilder("form");

            builder.MergeAttribute("action", formAction);
            builder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);
            builder.MergeAttribute("data-commandForm", "", true);
            if (htmlHelper.ViewContext.ClientValidationEnabled)
            {
                if (!htmlAttributes.ContainsKey("id"))
                {
                    builder.GenerateId(typeof(T).Name);
                }
            }
            builder.MergeAttributes(htmlAttributes);
            htmlHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
            var form = new CommandForm <T>(htmlHelper.ViewContext);

            form.Action     = action;
            form.Controller = controller;
            if (htmlHelper.ViewContext.ClientValidationEnabled)
            {
                htmlHelper.ViewContext.FormContext.FormId = builder.Attributes["id"];
            }
            return(form);
        }
예제 #2
0
        static CommandForm <T> FormHelper <T>(this AjaxHelper ajaxHelper, string formAction, string action, string controller, AjaxOptions ajaxOptions, IDictionary <string, object> htmlAttributes)
            where T : ICommand, new()
        {
            htmlAttributes = htmlAttributes ?? new Dictionary <string, object>();

            RenderCommandFormEventsScriptIfNotOnPage(ajaxHelper);

            var builder = new TagBuilder("form");

            builder.MergeAttribute("action", formAction);
            builder.MergeAttributes <string, object>(htmlAttributes);
            builder.MergeAttribute("method", "post");
            builder.MergeAttribute("data-commandForm", "", true);

            if (ajaxHelper.ViewContext.ClientValidationEnabled && !htmlAttributes.ContainsKey("id"))
            {
                builder.GenerateId(typeof(T).Name);
            }

            var idKey  = "id";
            var formId = builder.Attributes.ContainsKey(idKey) ? builder.Attributes["id"] : Guid.NewGuid().ToString();

            ajaxOptions = ajaxOptions ?? new AjaxOptions();

            ajaxOptions.OnSuccess  = string.IsNullOrEmpty(ajaxOptions.OnSuccess) ? "Bifrost.commands.commandFormEvents.onSuccess('#" + formId + "', data)" : ajaxOptions.OnSuccess;
            ajaxOptions.HttpMethod = string.IsNullOrEmpty(ajaxOptions.HttpMethod) ? "Get" : ajaxOptions.HttpMethod;

            if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
            {
                builder.MergeAttributes <string, object>(ajaxOptions.ToUnobtrusiveHtmlAttributes());
            }

            ajaxHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
            var form = new CommandForm <T>(ajaxHelper.ViewContext);

            form.Action     = action;
            form.Controller = controller;

            if (ajaxHelper.ViewContext.ClientValidationEnabled)
            {
                ajaxHelper.ViewContext.FormContext.FormId = formId;
            }
            return(form);
        }