How to call Ajax.BeginForm from a custom helper method?
Can I call Ajax.BeginFrom from a custom helper method ?
AjaxHelper is not available in a custom helper method, so I tried to pass the "Ajax" available 开发者_如何学JAVAin ViewPage to Helper method while calling it, but then in method, BeginForm is not available on that passed "Ajax" parameter.
You could instantiate it:
public static class HtmlExtensions
{
public static MvcHtmlString Foo(this HtmlHelper htmlHelper)
{
var ajaxHelper = new AjaxHelper(htmlHelper.ViewContext, htmlHelper.ViewDataContainer);
var form = ajaxHelper.BeginForm();
// ... use the ajaxHelper and htmlHelper
}
}
or if you are writing an extension method on AjaxHelper:
public static class AjaxExtensions
{
public static MvcHtmlString Foo(this AjaxHelper AjaxHelper)
{
var htmlHelper = new HtmlHelper(AjaxHelper.ViewContext, AjaxHelper.ViewDataContainer);
// ... use the ajaxHelper and htmlHelper
}
}
And don't forget the proper usings if you want to bring other extension methods into scope:
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Mvc.Ajax;
精彩评论