自动关闭自定义助手

发布于 2024-10-29 23:39:10 字数 2072 浏览 0 评论 0原文

我正在尝试实现自动关闭自定义助手。 我在下面找到了如何做到这一点:
自定义 html 帮助程序:创建具有“using”语句支持的帮助程序

我已经做了所有事情,除了: 我应该返回这里什么?

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 ???????
} 

我问是因为我有错误如下:

错误 9 'FieldContainer.BeginField(System.Web.Mvc.HtmlHelper, string)':并非所有代码路径都返回 a 值

所以我不知道要返回什么


好吧我已经创建了一切:

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();
        }
    }

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();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奈何桥上唱咆哮 2024-11-05 23:39:10

您的 BeginField 不需要返回 Action

Action 是一个委托,您需要传递给 DisposableHelper 构造函数。您将其设置为 () -> htmlHelper.BeginField(formName)。 DisposableHelper 的工作原理是记住您传入的两个回调 - 第一个是立即调用的启动标记 (BeginField),第二个是调用的结束标记 (EndField)处置DisposableHelper

更新:这就是你应该如何实现它。

a) 复制 DisposableHelper 类。

b) 编写 DisposableExtensions 扩展:

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

c) 更改 BeginField 声明以返回 void

public static void BeginField(...)

d) 添加 EndField方法来关闭标签。

e) 这样使用它:

using (Html.DisposableField("MyForm"))
{
    ...
}

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"))
{
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文