Search code examples
asp.net-mvchtml-helper

color class in custom helper


I have lot of custom control that have the property color (color of a button,color of a text ..etc) and css class

I want to create a class to have at the final in the view :

@Html.ContentBlock(new ISSCStyle() { Color = "Blue", class="" } )

I created a class like this :

public static class ISSCStyle
{
    public  static string color { get; set; }
}

what should I add to my class to get this behaviour in the view and what should I do in my custom control to reference this color or I dont need to?

a part of my custom control is :

var sb = new StringBuilder();
sb.AppendFormat(
    "<h2 class='{1}'>{0}</h2>",
    title,
    "Blue".Equals(GlobalProperties.color) ? "blueHeader" : string.Empty
    );
this.TextWriter.WriteLine(sb.ToString());

Solution

  • the solution is to create an abstract class

    public abstract class ISSCStyle
    {
        public  static string color { get; set; }
    }
    

    then create another class to inherit that class and to use it to create custom helper we pass an instance of the inherited class as parameter. and then in the view we will have

    @Html.ContentBlock(new ISSCStyle() { Color = "Blue", class="" } )