Example #1
0
        public static MvcHtmlString DynamicLink(this HtmlHelper helper, LinkAction action, string id, string caption, object viewData = null, string href = null)
        {
            href = HtmlHelpers.ActionToCommand(action, href);

            TagBuilder tag = new TagBuilder("a");

            tag.Attributes.Add("id", id);
            tag.Attributes.Add("href", href);
            tag.SetInnerText(caption);

            RouteValueDictionary htmlAttributes;
            RouteValueDictionary viewDataObj = HtmlHelper.AnonymousObjectToHtmlAttributes(viewData);

            if (viewDataObj.ContainsKey("htmlAttributes"))
            {
                htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(viewDataObj["htmlAttributes"]);
            }
            else
            {
                htmlAttributes = viewDataObj;
            }

            foreach (var attr in viewDataObj)
            {
                tag.Attributes.Add(attr.Key, attr.Value.ToString());
            }

            return(tag.ToMvcHtmlString(TagRenderMode.Normal));
        }
        public static MvcHtmlString DynamicButton(this HtmlHelper helper, LinkAction action, string id, string caption, bool printable = true, object viewData = null, string function = null)
        {
            function = HtmlHelpers.ActionToCommand(action, function);

            TagBuilder tag = new TagBuilder("input");

            tag.Attributes.Add("type", "button");
            tag.Attributes.Add("id", id);
            tag.Attributes.Add("value", caption);
            tag.Attributes.Add("onClick", function);

            RouteValueDictionary htmlAttributes;
            RouteValueDictionary viewDataObj = HtmlHelper.AnonymousObjectToHtmlAttributes(viewData);

            if (viewDataObj.ContainsKey("htmlAttributes"))
            {
                htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(viewDataObj["htmlAttributes"]);
            }
            else
            {
                htmlAttributes = viewDataObj;
            }

            tag.MergeAttributeValue("class", "btn", true);
            if (!printable)
            {
                tag.MergeAttributeValue("class", "d-print-none", true);
            }

            foreach (var attr in htmlAttributes)
            {
                tag.Attributes.Add(attr.Key, attr.Value.ToString());
            }

            return(tag.ToMvcHtmlString(TagRenderMode.SelfClosing));
        }