Hennie van den Berg
Hennie van den Berg

Reputation: 51

c# Extension methods and properties

I am using C# and MVC for my web applications and I like to make my life easy when coding so I did the following to use in my views. I use Devexpress. So to make my coding less in the view I did the following.

I created a class like this:

public class EditorUtils
{ 
    public static void Label(HtmlHelper htmlHelper, string CssClass, Object Model, String DataField)
    {
        LabelSettings lbSettings = new LabelSettings
        {
            Text = Text,
            Width = Width
        };
        lbSettings.ControlStyle.CssClass = CssClass;
        htmlHelper.DevExpress().Label(lbSettings).Bind(DataBinder.Eval(Model, DataField)).GetHtml();
    }
}

This is very easy for me because I only have now one line of code to render the label in the view.

I use it like this:

EditorUtils.Label(Html, "MyCssClass", Model, "FieldName1");
EditorUtils.Label(Html, "MyCssClass", Model, "FieldName2");

I like to organize my editors by putting them in a html table and I created a class like this:

public class HtmlUtils
{
    public static void BeginTable(HtmlHelper htmlHelper)
    {
        htmlHelper.ViewContext.Writer.Write("<table>");
    }

    public static void NewRow(HtmlHelper htmlHelper)
    {
        htmlHelper.ViewContext.Writer.Write("<tr><td>");
    }

    public static void NewColumn(HtmlHelper htmlHelper, string CssClass)
    {
        htmlHelper.ViewContext.Writer.Write(string.Format("</td><td class=\"{0}\">", CssClass));
    }

    public static void EndRow(HtmlHelper htmlHelper)
    {
        htmlHelper.ViewContext.Writer.Write("</td></tr>");
    }

    public static void EndTable(HtmlHelper htmlHelper)
    {
        htmlHelper.ViewContext.Writer.Write("</table>");
    }       

}

I use it like this in my view.

HtmlUtils.BeginTable(Html);
HtmlUtils.NewRow(Html);
EditorUtils.Label(Html, "MyCssClass", Model, "FieldName1");    
HtmlUtils.NewColumn(Html);
EditorUtils.Label(Html, "MyCssClass", Model, "FieldName2");
HtmlUtils.EndRow(Html);
HtmlUtils.EndTable(Html);

This will result in a table with 1 row and 2 columns with values.

Now to get to the point. I would like to optimize it further by doing someting like this.

HtmlUtils.BeginTable(Html);

EditorUtils.Label(Html, "MyCssClass", Model, "FieldName1").NewRow(Html); //this will create a new row and column and put the lable inside the field
EditorUtils.Label(Html, "MyCssClass", Model, "FieldName2").NewColumn(Html); //this will create a new column and put the lableiside the field

HtmlUtils.EndTable(Html);

I tried very hard to get a solution but to date could not find one. Can someone please sent me into the right direction.

Kind regards,

Upvotes: 2

Views: 205

Answers (1)

Mayhem50
Mayhem50

Reputation: 431

I've just written this up rather quickly without the ability to test. I hope it helps as I've mostly worked in web forms rather than mvc.

It might also be with checking out the following pages as well. (it's been a while since I've used these but I suspect they are better than using hard-coded strings..) HtmlGenericControl Class Table Class

public static class HtmlUtilsExtensions
{
  public static HtmlHelper BeginTable(this HtmlHelper htmlHelper)
  {
    return htmlHelper.ViewContext.Writer.Write("<table>");
  }

  public static HtmlHelper NewRow(HtmlHelper htmlHelper)
  {
     return htmlHelper.ViewContext.Writer.Write("<tr><td>");
  }

   public static HtmlHelper NewColumn(HtmlHelper htmlHelper, string CssClass)
   {
    return htmlHelper.ViewContext.Writer.Write(string.Format("</td><td class=\"{0}\">", CssClass));
   }

   public static HtmlHelper EndRow(HtmlHelper htmlHelper)
   {
    return htmlHelper.ViewContext.Writer.Write("</td></tr>");
   }

   public static HtmlHelper EndTable(HtmlHelper htmlHelper)
  {
    return htmlHelper.ViewContext.Writer.Write("</table>");
  }       

}

Then you could do something like this.

Html
.BeginTable()
.NewRow()
.Label(Html, "MyCssClass", Model, "FieldName1")
.NewColumn()
.Label(Html, "MyCssClass", Model, "FieldName2")
.EndRow()
.EndTable();


Html.BeginTable().NewRow()
.Label(Html, "MyCssClass", Model, "FieldName1")
.NewColumn()
.Label(Html, "MyCssClass", Model, "FieldName2")
.EndRow().EndTable();

Be sure to let us know how you go :D

Upvotes: 1

Related Questions