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
The solution is to pass in the
HtmlHelperobject as a parameter into your helper:You should also add the required
@usingstatements to your helper file to make the extension methods likeBeginFormwork:And then you need to call your helper method something like this: