是否可以在自定义助手中使用HtmlHelper对象?
问题描述:
任何人都可以建议是否可以在新的自定义Html帮助器中重新使用现有的HtmlHelper。例如:是否可以在自定义助手中使用HtmlHelper对象?
public static class GridHelper
{
public static string RenderGrid(this HtmlHelper helper, Object routeParams)
{
return HtmlHelper.BeginForm(routeParams);
}
}
上面是一个简单的例子,但本质上我想组格式化为一组HTML辅助呈现视图的一些逻辑&。这个视图会在几个地方使用,因此我想重新使用这些代码。然而,在我目前所有的尝试中,我一直无法访问'CheckBox'或'BeginForm'等方法。也许我错误地使用了HtmlHelper对象?
有谁知道这是否可以做到?
谢谢,马特
答
你下面使用补充的吗?
using System.Web.Mvc.Html;
您还可以使用通用的助手:
public static string RenderGrid<TModel, TProperty>(this HtmlHelper helper<TModel>, Expression<Func<TModel, TProperty>> displayExpression, Object routeParams)
但你并不需要调用静态类,你可以直接使用助手:
public static class GridHelper
{
public static string RenderGrid(this HtmlHelper helper, Object routeParams)
{
return helper.CheckBox("foo");
}
}
public static string RenderGrid<TModel, TProperty>(this HtmlHelper helper<TModel>, Expression<Func<TModel, TProperty>> expr, Object routeParams)
{
public static string RenderGrid(this HtmlHelper helper, Object routeParams)
{
return helper.CheckBoxFor(expr);
}
}
+0
我错过了使用。非常感谢您的帮助。 – Matt 2010-09-07 10:01:43
答
在你的榜样,我认为你需要做的:
public static class GridHelper
{
public static string RenderGrid(this HtmlHelper helper, Object routeParams)
{
return helper.BeginForm(routeParams);
}
}
没有得到足够的代表处,以编辑问题,在你的例子中轻微的打字让我暂时停下来。在你的返回声明中,你错误地在我猜测的“HtmlHelper”的末尾添加了“t”。 – 2010-09-07 09:01:21