w2bst2r
9/12/2018 - 6:41 PM

Dynamic button to be used by a partial biew

This is a dynamic button that can be created dynamically from a view by specifying the SmalLButtonModel object fields

@model Memberships.Areas.Admin.Models.SmallButtonModel

<a type="button" class="btn @Model.ButtonType btn-sm" href="@Url.Action(Model.Action)@Model.ActionParameters">
    <span class="glyphicon glyphicon-@Model.Glyph" aria-hidden="true"></span>
    <span class="sr-only">@Model.Text</span>
</a>

//-----------------------------------------------------------------------------------------------------------

   public class SmallButtonPartial
    {
        public string Action { get; set; }
        public string Text { get; set; }
        public string Glyph { get; set; }
        public string ButtonType { get; set; }
        public int? Id { get; set; }
        public int? ItemId { get; set; }
        public int? ProductId { get; set; }
        public int? SubscriptionId { get; set; }

        public string ActionParameters
        {
            get
            {
                var param = new StringBuilder("?");
                if (Id != null && Id > 0) param.Append(String.Format("{0}={1}&", "Id", Id));
                if (Id != null && ItemId > 0) param.Append(String.Format("{0}={1}&", "ItemId", ItemId));
                if (Id != null && ProductId > 0) param.Append(String.Format("{0}={1}&", "ProductId", ProductId));
                if (Id != null && SubscriptionId > 0) param.Append(String.Format("{0}={1}&", "SubscriptionId", SubscriptionId));
                return param.ToString().Substring(0, param.Length - 1);
            }
        }
    }