要从自己的HtmlHelper中访问HtmlHelper方法,您可以按照以下步骤操作:
using System.Web.Mvc;
public class MyHtmlHelper : HtmlHelper
{
public MyHtmlHelper(ViewContext viewContext, IViewDataContainer viewDataContainer)
: base(viewContext, viewDataContainer)
{
}
public MvcHtmlString MyCustomMethod()
{
// 在这里编写您的自定义方法代码
return MvcHtmlString.Create("My Custom Method");
}
}
@using MyNamespace.HtmlHelpers
@model MyModel
@{
MyHtmlHelper myHtmlHelper = new MyHtmlHelper(Html.ViewContext, Html.ViewDataContainer);
}
<!DOCTYPE html>
<html>
<head>
<title>My View</title>
</head>
<body>
@myHtmlHelper.MyCustomMethod()
</body>
</html>
在这个例子中,我们创建了一个名为MyHtmlHelper的自定义HtmlHelper类,并在其中添加了一个名为MyCustomMethod的方法。然后,在视图中,我们使用MyHtmlHelper类的实例来访问MyCustomMethod方法。这样,您就可以在自己的HtmlHelper中访问自定义的HtmlHelper方法了。
领取专属 10元无门槛券
手把手带您无忧上云