コード例 #1
0
        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));
        }