3

I want to create some global helper functions. I understood that i must place them in a .cshtml file in App_Code. I created this file:

@helper CreatePostForm(string action, string controller, string id, params string[] hiddens)
{       
    using (BeginForm(action, controller, System.Web.Mvc.FormMethod.Post, new { id = id }))
    {
        @Html.AntiForgeryToken()
        foreach(string hidden in hiddens)
        {
            @Html.Hidden(hidden)   
        }
    }
}

The problem is that BeginForm and AntiForgeryToken methods are nor recognized. How to make it right?

PS: i am using .net 4.5, asp.net mvc 4

1

2 Answers 2

3

The solution is to pass in the HtmlHelper object as a parameter into your helper:

@helper CreatePostForm(HtmlHelper html, 
                       string action, string controller, string id, 
                       params string[] hiddens)
{       
    using (html.BeginForm(action, controller, FormMethod.Post, new { id = id }))
    {
        @html.AntiForgeryToken()
        foreach(string hidden in hiddens)
        {
            @html.Hidden(hidden)   
        }
    }
}

You should also add the required @using statements to your helper file to make the extension methods like BeginForm work:

@using System.Web.Mvc.Html
@using System.Web.Mvc

And then you need to call your helper method something like this:

@MyHelpers.CreatePostForm(Html, "SomeAtion", "SomeContoller" , "SomeId")
Sign up to request clarification or add additional context in comments.

5 Comments

I still can't access those methods on html parameter. Secondly, i can use Html in that cshtml. The problem is with those extension methods( BeginForm, AntiForgeryToken).
These are all extension methods have you added the required usings at the top of your helper file? @using System.Web.Mvc.Html @using System.Web.Mvc
I don't really like passing Html as an input. But adding this to try and make an extension didn't work on my helper. Is there any other way? (Other than a real extension class, which is harder to build.)
@ashes999 There is no other way. If you want to a global helper method and use Html then you need to pass in a HtmlHelper. Or you need to create a proper extension class and create a regular HtmlHelper extension.
You don't have to pass anything to global helper functions. See my answer.
1

You don't have to pass the HtmlHelper object as a parameter. Just put this in your .cshtml file residing in App_Code:

@functions {
    private new static HtmlHelper<dynamic> Html => ((WebViewPage)WebPageContext.Current.Page).Html;
}

Other useful members are:

private static UrlHelper Url => ((WebViewPage)WebPageContext.Current.Page).Url;
private static ViewContext ViewContext => ((WebViewPage)WebPageContext.Current.Page).ViewContext;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.