自动关闭自定义助手
我正在尝试实现自动关闭自定义助手。 我在下面找到了如何做到这一点:
自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
BeginField
不需要返回Action
。Action 是一个委托,您需要传递给
DisposableHelper
构造函数。您将其设置为() -> htmlHelper.BeginField(formName)
。 DisposableHelper 的工作原理是记住您传入的两个回调 - 第一个是立即调用的启动标记 (BeginField
),第二个是调用的结束标记 (EndField
)处置DisposableHelper
。更新:这就是你应该如何实现它。
a) 复制
DisposableHelper
类。b) 编写
DisposableExtensions
扩展:c) 更改
BeginField
声明以返回void
:d) 添加
EndField
方法来关闭标签。e) 这样使用它:
Your
BeginField
does not need to returnAction
.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 theDisposableHelper
.UPDATE: This is how you should implement it.
a) Copy the
DisposableHelper
class.b) Write an extension to
DisposableExtensions
:c) Change your
BeginField
declaration to returnvoid
:d) Add
EndField
method to close the tag.e) Use it this way: