0

I am trying to implement auto closing custom helper. I have found how to to this below:
Custom html helpers: Create helper with "using" statement support

I have done everything except: what i am supossed to return here?

public static Action BeginField(this HtmlHelper htmlHelper, string formName)
{
  string result = string.Format("<div class=\"FullWidthForm\"><span>{0}</span>", formName);
  result += "<ul class=\"Left\">";
  htmlHelper.ViewContext.HttpContext.Response.Write(result);
  return ???????
} 

I am asking because i have error as follows:

Error 9 'FieldContainer.BeginField(System.Web.Mvc.HtmlHelper, string)': not all code paths return a value

so i do not have idea what to return


Ok I have created everything how ever:

public static class DisposableExtensions
    {
        public static IDisposable DisposableField(this HtmlHelper htmlHelper, string formName)
        {
            //'HtmlHelpers.DisposableHelper' does not contain a constructor that takes 2 arguments
            return new DisposableHelper(
                () => htmlHelper.BeginField(formName),
                () => htmlHelper.EndField());
        }

        public static void BeginField(this HtmlHelper htmlHelper, string formName)
        {
            htmlHelper.ViewContext.HttpContext.Response.Write("<ul><li>" + formName + "</li>");

        }
        public static void EndField(this HtmlHelper htmlHelper)
        {
            htmlHelper.ViewContext.HttpContext.Response.Write("</ul>");
        }

    }

    class DisposableHelper : IDisposable
    {
        private Action end;
        // When the object is create, write "begin" function
        // make this public so it can be accessible
       public DisposableHelper(Action begin, Action end)
        {
            this.end = end;
            begin();
        }
        // When the object is disposed (end of using block), write "end" function
        public void Dispose()
        {
            end();
        }
    }
Community
  • 1
  • 1
cpoDesign
  • 8,953
  • 13
  • 62
  • 106

1 Answers1

2

Your BeginField does not need to return Action.

Action is a delegate, a callback you need to pass to the DisposableHelper constructor. You will setup it as () -> htmlHelper.BeginField(formName). DisposableHelper works by remembering the two callbacks you pass in - first to start the tag (BeginField) which is called immediately, second to end the tag (EndField) which is called on disposal of the DisposableHelper.

UPDATE: This is how you should implement it.

a) Copy the DisposableHelper class.

b) Write an extension to DisposableExtensions:

public static class DisposableExtensions
{
    public static IDisposable DisposableField(this HtmlHelper htmlHelper, string formName)
    {
        return new DisposableHelper(
            () => htmlHelper.BeginField(formName),
            () => htmlHelper.EndField()
        );
    }
}

c) Change your BeginField declaration to return void:

public static void BeginField(...)

d) Add EndField method to close the tag.

e) Use it this way:

using (Html.DisposableField("MyForm"))
{
    ...
}
František Žiačik
  • 7,511
  • 1
  • 34
  • 59
  • 1
    +1 I think the answer here could equally be 'read the other answer properly'... or perhaps I am just feeling grumpy today... – Andras Zoltan Apr 05 '11 at 09:41
  • Yep i did read that but there is how to write it into the return,Maybe my head just does not get it – cpoDesign Apr 05 '11 at 13:35